7

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.

Bartley
  • 290
  • 2
  • 7
  • 21
user1567956
  • 131
  • 2
  • 6
  • 13

4 Answers4

17

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;
}
Cyrille
  • 25,014
  • 12
  • 67
  • 90
  • and then how to given cell text values and didSelect row method – user1567956 Aug 06 '12 at 06:45
  • 2
    All datasource/delegate methods have a `tableView` parameter. Not only `numberOfRowsInSection`, but also `cellForRowAtIndexPath` or `didSelectRowAtIndexPath`. – Cyrille Aug 06 '12 at 06:47
2

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

Community
  • 1
  • 1
Kapil Choubisa
  • 5,152
  • 9
  • 65
  • 100
2

Please have a look.

First create two tableview in interface builder and then connect with two IBOutlet variables and set delegate and datasource for both the tableviews.

In Interface file

 -IBOutlet UITableView *tableView1;
 -IBOutlet UITableView *tableView2;

In Implementation file

 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
  {
       if (tableView==tableView1)
       {
        return 1;
       }
       else 
       {
         return 2;
       }
  }

 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
 {
    if (tableView==tableView1)
    {
         return 3;
    }
    else
    {
         if (section==0)
          {
            return 2;
          }
          else
          {
            return 3;
          }
     }
  }

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
  {
        static NSString *CellIdentifier = @"Cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) 
            {
                  cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
            }

    if (tableView==tableView1)
         {
           //cell for first table
         }
    else 
         {
           //cell for second table
          }
    return cell;
 }

Use this code. hope helps

Mahendra
  • 425
  • 2
  • 9
1

It is possible,look at the reference code here: http://github.com/vikingosegundo/my-programming-examples

Please also refer this page : 2 tableview on a single view

Community
  • 1
  • 1
Karthi
  • 13,624
  • 10
  • 53
  • 76