问题
I have two instances of UITableView in one app, and the problem is that I don't know how to define what each table needs to display. Here's the code:
.h :
{
NSArray *array1;
NSArray *array2;
IBOutlet UITableView *table1;
IBOutlet UITableView *table2;
}
.m :
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [array1 count];
}
- (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] autorelease];
}
cell.textLabel.text = [array1 objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
}
This code is working for just one table. I don't know how to set up the other table with array2. Have you got any ideas, people?
回答1:
All you need to do is check which UITableView
you are setting up in the delegate/datasource
methods. Try:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; {
if(tableView == table1){
return 1; // The number of sections in table1;
}
else if(tableView == table2){
return 1; // The number of sections in table2;
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section; {
if(tableView == table1){
return [array1 count];
}
else if(tableView == table2){
return [array2 count];
}
}
- (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] autorelease];
}
if(tableView == table1){
cell.textLabel.text = [array1 objectAtIndex:indexPath.row];;
}
else if(tableView == table2){
cell.textLabel.text = [array2 objectAtIndex:indexPath.row];;
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath; {
UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
}
Hope that Helps!
回答2:
On UITableViewDataSource methods you have to compare your ivars with the delegates.
like this:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if([tableView isEqual:table1])
{
return [array1 count];
}
else if([tableView isEqual:table2])
{
return [array2 count];
}
else
{
return 0;
}
}
Do this for every method in the callback.
But I would recommend having only one tableView and load different contents in it based on some flag. You would have to call [tableView reloadData]
in order for this to function and set a flag. Then you would change the above code like this if([flag isEqualToString:@"table1"]) { //code for table1 }
Unless you have two tables on the same View. Then the first method is what you should do.
回答3:
The normal way to do this is to create separate datasource/delegate objects for each table view. They don't have to be separate classes. They can be two instances of the same class.
来源:https://stackoverflow.com/questions/9318098/how-to-deal-with-two-tableview