views:

868

answers:

3

Is there any way to access the UITableView from within a UITableViewCell? (if the cell belongs to that tableView?

+4  A: 
self.superview

Ought to do the trick.

jbrennan
This works perfectly. Awesome. Thanks, man.
Michael Grinich
To be safe, you might want to add a '[self.superview isKindOfClass:[UITableView class]]' check, just in case the view hierarchy changes in the future.
Frank Szczerba
Frank's suggestion is definitely a good idea.
jbrennan
+2  A: 

You have to add a reference back to the UITableView when you construct the table view cell.

However, almost certainly what you really want is a reference to your UITableViewController... that requires the same thing, set it as a delegate of the cell when you build the cell and hand it to the table view.

An alternate approach if you are wiring up actions is to build the cells in IB, with the table view controller as the files owner - then wire up buttons in the cell to actions in the table view controller. When you load the cell xib with loadNibNamed, pass in the view controller as the owner and the button actions will be wired back to the table view controller.

Kendall Helmstetter Gelner
+1  A: 

If you have custom classes for your UITableViewCells, you can add an id type variable in your cell's header, and synthesize the variable. After you set the variable when you load the cell, you are free to do what you please with the tableview or any other higher view without much hassle or overhead.

cell.h

 // interface
 id root;

 // propery 
 @property (nonatomic, retain) id root;

cell.m

@synthesize root;

tableviewcontroller.m

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  // blah blah, traditional cell declaration
  // but before return cell;
  cell.root = tableView;
}

Now you can call any of the tableview's methods from within your cell using the root variable. (e.g., [root reloadData]);

Ah, takes me back to the good old days of flash programming.