I am using two UITableView
s 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.

- 290
- 2
- 7
- 21

- 131
- 2
- 6
- 13
4 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;
}

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

- 1
- 1

- 5,152
- 9
- 65
- 100
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

- 425
- 2
- 9
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