Alex Rivera | Logout

iphone - access uitableviewcontroller from uitableviewcell

Asked 2009-05-06T09:17:22.897
12

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!

Edit
Report

2 Answers

7

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.

answered 2009-05-06T10:24:27.387
4

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`
answered 2013-03-25T09:41:20.583

Your Answer