beeudoublez
beeudoublez

Reputation: 1242

Using the same UITableViewController on multiple screens

In my app, I'm showing an Activity feed in a couple different pages. So I'm not duplicating code, this feed has it's own UITableViewController code. For each page, I'd like to specify an area that this feed (Table) should display, and use the same UITableViewController code in each place. How would I do this? All the logic/code should be handled by that UITableViewController, not the base VC

I tried creating a specific UITableViewController (FeedViewController) class that contained all the row/section/tapping logic. Then, for each page I dropped in an UITableView, allocated the FeedViewController, and set the delegate/datasource to the FeedViewController. This works great except when I want to push on a new screen or call reloadData (based on how the user interacted with the table). I have to send a delegate message back to the base VC, like 'pushThisView' or 'reloadThisTable'. This seems dirty.

How can I make it so the embedded UITableViewController can reload it's own table, and push/dismiss new views onto the parent VC?

Upvotes: 1

Views: 239

Answers (1)

Bartosz Ciechanowski
Bartosz Ciechanowski

Reputation: 10333

You should use Container View Controller as described in UIViewController's class reference. This allows you to embed any UIViewController inside any parent UIViewController with custom frame and transition. While you will find everything you need to know in the documentation and in this answer, here's a code sample that will set you up:

FeedViewController *feedViewController = ... // create your view controller
[self addChildViewController:feedViewController];
feedViewController.view.frame = ... // frame for view
[self.view addSubview: feedViewController.view];

[feedViewController didMoveToParentViewController:self];

Upvotes: 3

Related Questions