Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use more than two UITableView in single view controller in iphone

I am using two UITableViews in a UIViewController, how can I populate the row and cells in both tableviews? When I give for the second tableview it says the duplicate declaration of the number of rows in section etc.

like image 623
user1567956 Avatar asked Aug 06 '12 06:08

user1567956


2 Answers

That's why the dataSource/delegate methods have a tableView parameter. Depending on its value, you can return different numbers/cells/...

- (void)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == _myTableViewOutlet1)
        return 10;
    else
        return 20;
}
like image 154
Cyrille Avatar answered Sep 17 '22 10:09

Cyrille


All of your UITableViewDelegate and UITableViewDatasource methods will be implemented only once. You just need to check for which table view the method is being called.

if (tableView == tblView1) {
    //Implementation for first tableView
}
else {
    //Implementation for second tableView
}

this will work in all of TableView's delegate and datasource methods as tableView is common parameter in all of your methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {}

Look here and here

This Link also has the solution of your issue.

Hope this helps

like image 27
Kapil Choubisa Avatar answered Sep 19 '22 10:09

Kapil Choubisa