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

16,491

Solution 1

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;
}

Solution 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

Solution 3

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

Solution 4

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

Share:
16,491
user1567956
Author by

user1567956

Updated on June 15, 2022

Comments

  • user1567956
    user1567956 about 2 years

    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.