vince
vince

Reputation: 161

Two UITableView in the same view

I want to know if it's allowed to use Multiple UItableView in the same View (i don't see any thing in the Apple's Human Interface Guidelines) and if it's OK, How to load different DataSource in viewDidLoad for each UITableView?

Upvotes: 16

Views: 15135

Answers (6)

Ramu Pasupuleti
Ramu Pasupuleti

Reputation: 886

You can have multiple table views in a single view. Add tags to each table view and with use of tableview.tag you can load data into tableviews separately.

Example:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
     if (tableView.tag == x) {
        //code to load table view with tag value x

        }
        else{
        //code to load second table
        }
        return cell;

    }

Upvotes: 1

tinku
tinku

Reputation: 31

You can set tag for each table. Then apply on that condition in tableview delegate method, for example:

myTable.tag=12;

Upvotes: 3

vikingosegundo
vikingosegundo

Reputation: 52227

IMO the cleanest solution would be to have one controller for each tableview.

If you use one controller for n tableview, you will have to use if-statemenst in many places, in

  • – numberOfSectionsInTableView:
  • – tableView:numberOfRowsInSection:
  • – tableView:titleForHeaderInSection:

Basically in all UITableViewDatasource-Protocol methods that you will need to implement.

So if you need to change something, you have to change it in many places.

If you use one controller class for one tableview, you won't have to check at all.

  1. write a controller class for every tableview, make it conforming to the UITableViewDatasource protocol
    • implement the protocol methods you will need. at least
      • – numberOfSectionsInTableView:,
      • – tableView:numberOfRowsInSection:,
      • – tableView:cellForRowAtIndexPath:
  2. call -setDataSource:for every tableview to an object of the right controller class

I wrote an example code: https://github.com/vikingosegundo/my-programming-examples/tree/master/TwoTableViews

TwoTableViewsViewController.m

- (void)viewDidLoad {
    [super viewDidLoad];
    if (firstController == nil) {
        firstController = [[FirstTVContoller alloc] init];
    }
    if (secondController == nil) {
        secondController = [[SecondTVController alloc] init];
    }
    [firstTable setDataSource:firstController];
    [secondTable setDataSource:secondController];

    [firstTable setDelegate:firstController];
    [secondTable setDelegate:secondController];
    firstController.view = firstController.tableView;
    secondController.view = secondController.tableView;
}

Upvotes: 5

Black Frog
Black Frog

Reputation: 11713

To make life easier, you can pass in two different delegate to the UITableView. If you pass in the same delegate, you will have to do a lot of if statements. By creating two different delegate it will allow your code to be a lot cleaner.

Upvotes: 2

dredful
dredful

Reputation: 4388

Yes you can. The issue is that each UITableView will use the same UITableViewDataSource and UITableViewDelegate. Therefore you must determine which table view you are working with in each of the necessary delegate methods.

For example:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    // make bigger rows

    if (tableView == myBigRowTableView)
    {
        // make bigger rows
        return 127;
    } else if (tableView == mySmallRowTableView) {
        // make smaller rows
        return 20;
    } else {
        return 30;
    } 
}

Upvotes: 5

Christian
Christian

Reputation: 1714

You can most certainly have multiple table views. You would want to make sure you keep a pointer around to each one, then in your data source methods, you would do something like this:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
     if(tableView == tableViewOne)
           return 5;
     else //if (tableView == tableViewTwo)
           return 3;
}

This would be the same for all delegate / data source methods, which is why they give you which table view as a parameter.

Upvotes: 20

Related Questions