Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 374
vince Avatar asked Apr 21 '11 17:04

vince


4 Answers

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.

like image 93
Christian Avatar answered Oct 10 '22 17:10

Christian


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;
    } 
}
like image 43
dredful Avatar answered Oct 10 '22 17:10

dredful


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;
}
like image 44
vikingosegundo Avatar answered Oct 10 '22 16:10

vikingosegundo


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

myTable.tag=12;
like image 40
tinku Avatar answered Oct 10 '22 16:10

tinku