How to use more than two UITableViews in a single view controller in iphone

I am using two UITableView in a UIViewController , how can I fill the row and cells in both table views? When I give for the second view of the table, it talks about double declaring the number of rows in a section, etc.

+8
iphone xcode uitableview
source share
4 answers

This is 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; } 
+17
source share

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

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

this will work in all delegate methods of TableView and datasource, since tableView is a 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 a solution to your problem.

Hope this helps

+2
source share

Please look.

First create two table views in the interface builder, and then connect to the two IBOutlet variables and set the delegate and data source for both table views.

In the interface file

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

In the 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. hopes help

+2
source share

Maybe look at the link code here: http://github.com/vikingosegundo/my-programming-examples

Also see this page: 2 tableviews in one view

+1
source share

All Articles