Wallace Banter
Wallace Banter

Reputation: 165

UITableViewController functionality in normal UIViewController

I'm currently having a UITableView added to a UIView managed by a normal UIViewController (all defined in IB) Now I want to add some of the UITableViewController functionality to this UITableView (automatic scrolling when textfield is selected, refreshcontrol managed, auto insets etc.). How can I manage this without using a UITableViewController for the whole layout (I want to add other stuff to my view too so UITableViewController isn't a option) and without recreating the entire functionality by myself?

Upvotes: 2

Views: 367

Answers (2)

Mike Weller
Mike Weller

Reputation: 45598

If you do go with a regular view controller, just remember to flash the scroll bars and deselect the currently selected row in the appropriate places. This is a pet peeve of mine. UITableViewController does this by default:

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    [self.tableView flashScrollIndicators];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    [self.tableView deselectRowAtIndexPath:self.tableView.indexPathForSelectedRow
                      animated:animated];
}

Upvotes: 0

Leon Lucardie
Leon Lucardie

Reputation: 9740

It's possible to set up a UITableViewController for a already initialized UITableView even if you're using a normal UIViewController to manage the view containing it. Create a UITableViewController, set the already existing UITableView as the managed tableview and then add it as a child UIViewController to your current viewcontroller.

Example (for use in the viewDidLoad method):

UITableViewController *tableViewController = [[UITableViewController alloc] initWithStyle:yourTableView.style];
tableViewController.tableView = yourTableView;
[self addChildViewController:tableViewController];

This will add all the functionality you want while still supporting the addition of more components to your view.

Upvotes: 3

Related Questions