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
?
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.
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;
}
}
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.
UITableViewDatasource
protocol
– numberOfSectionsInTableView:
,– tableView:numberOfRowsInSection:
, – tableView:cellForRowAtIndexPath:
-setDataSource:
for every tableview to an object of the right controller classI 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;
}
You can set tag for each table. Then apply on that condition in tableview delegate method, for example:
myTable.tag=12;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With