Rukia Kuchiki
Rukia Kuchiki

Reputation: 120

Two UITableViews in the same UIViewController

Im trying to have 2 table views on the same view. They both are within the same UIViewController which implementes the UITableViewDelegate and Datasource. one of the tableviews is static and the other is dynamic. The dynamic table view is loading just fine using the methods from its datasource, but the static one is showing up blank. Normally I erase the datasource methods from the controller so the static TableView doesn't override what's already done on the storyboard, but now I can't because those methods are being used by the dynamic TableView.

How can I have both of then under the same controller?

Upvotes: 1

Views: 1331

Answers (3)

rdurand
rdurand

Reputation: 7410

Here is the easiest option, that let's you keep a single VC for your delegate/datasource :

Set both tableviews delegate and datasource to your UIViewController.

Ctrl-drag from one of the static cells to your .h file, and create an IBOutlet collection ( called here staticCellsCollection). Add each static cell to this collection, careful with the order, it will be important.

Implement cellForRowAtIndexPath: this way :

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    if (tableView == _dynamicTableView) {
        // Do your dynamic thing
    }

    else if (tableView == _staticTableView) {
        // Return the static cells one by one
        // Here the static TV has only one section, and all cells are in staticCellsCollection
        return staticCellsCollection[indexPath.row];
    }

}

You also need to adapt numberOfRowsInSection and numberOfSectionInTableView but this is pretty basic (check which table is asking, and return appropriate values, for example staticCellsCollection.count for the number of rows of the static TV).

You may need to adapt this, for example if you want multiple sections in your static TableView, you should create an IBOutletCollection for each section, and handle the number of rows/sections accordingly, and return the correct cells. Anyway, this is pretty straightforward to implement once you get the idea.

Upvotes: 1

Duncan C
Duncan C

Reputation: 131398

As Putz says, you can set up your view controller to manage 2 table views by setting up your data source and delegate methods to check the table view that's passed in.

However, I don't think you can use static table views with anything but a UITableViewController, and a table view controller only knows how to manage a single table view.

The trick is to add 2 container views to your view controller that will contain 2 table views, and embed a different UITableViewController into each container view. Then each table view is managed by it's own table view controller. I have a project on github that does exactly this: https://github.com/DuncanMC/test

I've set up protocols that the parent view controller and the 2 table view controllers use to communicate, although you don't have to do that if your needs are simpler.

Upvotes: 1

Putz1103
Putz1103

Reputation: 6211

You can have two tableView's use the same delegate functions. It's not amazingly pretty, but you can:

- (UITableViewCell *)tableView:(UITableView *)inTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(inTableView == tableView1)
    {
        ...
    }
    else if
        ...
}

Put that type of logic into every tableView delegate function.

Upvotes: 0

Related Questions