Question

I have a structure like this....

UITableViewController -> UITableViewCell -> UIView

I need the UIView to access a HashTable (NSMutableDictionary) in the UITableViewController

Is there a way to access the ViewController simply from the ViewCell (using [ViewCell superview] obviously won't work) ....Do I need to go down through the AppDelegate using [[UIApplication sharedApplication] delegate]?

Thanks!

Was it helpful?

Solution

I usually maintain a weak reference from my UIView to my UIViewController if I need one, usually by creating a method something like this:

-(MyView*)initWithController:(CardCreatorViewController*) aController andFrame:(CGRect)aFrame
{
    if (self = [super initWithFrame:aFrame]) 
    {
       controller = aController;
       // more initialisation here
    }

    return self;
}

You could also use a delegate pattern if you want a more decoupled solution. I tend to think this is overkill for a view and its controller, but I would use it with a system of controllers and subcontrollers.

OTHER TIPS

This should do the trick:

UITableView *tv = (UITableView *) self.superview.superview;
UITableViewController *vc = (UITableViewController *) tv.dataSource;

You can create a category for this:

@implementation UITableViewCell (FindTableViewController)

- (id<UITableViewDataSource>)tableViewController
{
    UIView *view = self;
    while (!(view == nil || [view isKindOfClass:[UITableView class]])) {
        view = view.superview;
    }

    return ((UITableView *)view).dataSource;
}

@end

Then you can simply access self.tableViewController from the cell itself (assuming you have included this category). You may need to cast it to your table view controller's class tho.

Since the UITableViewCell is somewhere in the view hierarchy, you can access your root view by retrieving view.superview until you get it. If you don't want to add any properties to your view, you can access its controller through the view's nextResponder property. You would have to cast it to whatever class you need, of course, and it may not be the cleanest use of the property. It's a quick-n-dirty hack to get to it.

If you're looking for something you can show your children though, I'd aim for going through your app delegate, or if your view controller happens to be a singleton, just implement the singleton design pattern and access it through that.

Some modification from Kare Morstol answer :

The hierarchy of tableviewcell is in iOS 5.0(my test version)

cell.superview = tableview  
cell.superview.superview = UIViewControllerWrapperView  

So, use cell.superview to get tableview. And the tableview and tableviewController has a relation of delegate, dataSource in default.
You can get tableviewController reference by tableview.delegate or tableview.dataSource.

UITableView *tableView = cell.superview;  
UITableViewController *tableViewController = tableView.delegate; // or tableView.dataSource`
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top