How to call UItableview delegate methods. When is the tableview in the "for" loop?

// I create a tableview in for the loop . after a single cycle, you must call the tableview delegate / data source methods. But here the methods (delegate / datasource) are called after the finish loop

// Note : I show all tables in scrollview based on page controller

// here is my code

for(int i=0; i < 10; i++) { table_obj[i] = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 700, 500)]; table_obj[i].separatorStyle = UITableViewCellSeparatorStyleNone; table_obj[i].separatorColor = [UIColor clearColor]; table_obj[i].delegate = self; table_obj[i].dataSource = self; table_obj[i].backgroundColor = [UIColor clearColor]; [scrollview addSubview:table_obj[i]]; [table_obj[i] reloadData]; } 

// data is loaded only in the 10th table. not in all tables

+4
source share
3 answers

If after that you reload the table, then it should work fine.

I think the opinions you add to this, instead of adding this to your - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath mehod, try using a custom cell concept. and reload the table when you switch from one type of table to another table view.

Custom cell tutorial.

+2
source

I think you should use the tag ( table_obj[i] = i ) to judge that the delegate is being called using the View table.

eg:

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { switch (tableView.tag) { case 0: //do something break; case 1: //do something break; ... ... default: break; } } 
+2
source

This is because when you set the / dataSource delegate for yourself, in the loop it gets the excess and will have the last in the array as the current delegate / dataSource.

You can set "tags" in tableViews, and then in callbacks do manipulations based on the tableView tag.

Another option would be to loop through the array of tableViews to get the current one, but this is not very efficient, so I think the first option is better.

+2
source

All Articles