ebandersen
ebandersen

Reputation: 2352

Objective-c: How to find UITableViewCell's containing UITableView

I've subclassed UITableView with some specific properties that I'd like to be able to access in a subclass of UITableViewCell. Is there something like cell.containingTableView that I'm just missing?

Upvotes: 0

Views: 425

Answers (2)

Gabriele Petronella
Gabriele Petronella

Reputation: 108121

You may think of using

self.superview

but it's VERY fragile, so please DON'T!.

You'd better pass a reference to the UITableView explicitly to the cell.

In your cell declare a property

@property (nonatomic, weak) UITableView *parentTableView;

and assign it in the tableView:cellForRowAtIndexPath: data source method.

Finally if your purpose is just to call some methods on the parent controller, you can use a proper delegation pattern and define your own protocol

@protocol CellDelegate <NSObject>
- (void)aMethodThatINeedToCall:(id)whatever;
@end

declare a delegate property

@property (nonatomic, weak) id<CellDelegate> delegate;

and make your UITableViewController conform to that

@interface MyTableViewController : UITableViewController <CellDelegate>
...
@end

@implementation

...

- (void)aMethodThatINeedToCall:(id)whatever {
    // do stuff
}

...

@end

Upvotes: 1

user1078170
user1078170

Reputation:

Really you don't want your views to know much of anything about their parents. The thing to do is subclass UITableViewCell, give it some properties to hold the information you want to pass to it and then let your UITableViewController do so in tableView:cellForRowAtIndexPath:

Upvotes: 4

Related Questions