Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>IMHO, this is a poor design. For starters, your cell shouldn't need a reference to the view controller that the table it's in is in (read that twice, it barely makes sense just because the very idea of it is confusing). You have a strong reference to this view controller. So what happens when the OS tries to deallocate your view controller? It will never be able to, because the table view cell as a strong reference to it, keeping its retain count at 1. The same situation holds true for the cell. You risk running into a retain cycle here. Generally, child views should have weak references to their parents.</p> <p>But this isn't even really a true parents/child relationship. I would suggest instead an approach like this, which all occurs in your view controller that contains the table view:</p> <pre><code>- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // Assuming you set a reuse identifier "cellId" in the nib for your table view cell... MyCell *cell = (MyCell *)[tableView dequeueReusableCellWithIdentifier:@"cellId"]; if (!cell) { // If you didn't get a valid cell reference back, unload a cell from the nib NSArray *nibArray = [[NSBundle mainBundle] loadNibNamed:@"MyCell" owner:nil options:nil]; for (id obj in nibArray) { if ([obj isMemberOfClass:[MyCell class]]) { // Assign cell to obj, and add a target action for the checkmark cell = (MyCell *)obj; [cell.checkMarkButton addTarget:self action:@selector(checkPressed:) forControlEvents:whateverEventYouWant]; break; } } } // Set the tag of the cell here, since we may get a different cell back from the reuse queue cell.checkMarkButton.tag = indexPath.row; return cell; } </code></pre> <p>Now set up the method for the clicking of the checkmark button</p> <pre><code>- (void)checkPressed:(id)sender { UIButton *checkmark = (UIButton *)sender; // This will give you the row of the checked button int checkedCellRow = checkmark.tag; NSIndexPath *indexPath = [NSIndexPath indexPathForRow:checkedCellRow inSection:0]; // Now you can grab a reference to that cell if you need to MyCell *cell = [self.tableView cellForRowAtIndexPath:indexPath]; } </code></pre> <p>This way, you keep all of the controller-related stuff in your controller class (i.e. how to handle the checkmark button being pressed), and you don't need to deal with this whackiness of referencing the view controller of your cell's table.</p> <p><strong>EDIT:</strong> I guess I should also help answer your questions...First of all, if you're saying that in your refreshVisibleViewForCell method, you're getting a nil value for self.myTableView, are you sure it is hooked up properly in IB? Even if it's hooked up, click the little x to unhook it and hook it up again to be sure. Also make sure you've @synthesized your myTableView property. Without seeing more code, an IB issue is my best guess as to why you're getting a nil value for tableView. A nil value here will result in a nil indexPath and selectedCell, also. As for your big question, you can access <em>properties</em> of objects within your view controller. Those properties can, of course, be objects. So in your example, if you have a tag property on selectedCell, you can access it from anywhere that you have a <em>valid</em> reference to selectedCell. If selectedCell is nil, the property will be nil. @class is better suited for header files. For instance, if you wanted to make your custom cell a property of your view controller, you might say:</p> <pre><code>#import &lt;UIKit/UIKit.h&gt; @class MyCell; @interface RootViewController : UIViewController @property (nonatomic, strong) MyCell *cell; @end </code></pre> <p>Then, in your implementation file, you would actually import MyCell.h. Giving the @class forward declaration just keeps you from having to import all of the details about the MyCell class in your header file. The header doesn't need to know about all of the properties and methods of MyCell, just that you intend on using it in the implementation file. So you @class in the header, #import in the implementation.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload