在单个视图上的2个tableview

我需要一个例子或解释如何填充在同一个视图上的两个表视图。 我需要了解“cellForRowAtIndexPath”方法,有人可以提供一个例子,应该如何代码?

我的意思是如何识别哪个表视图?

谢谢

以下是我的cellForRowAtIndexPath方法:

// Customize the appearance of table view cells. - (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]; } // Configure the cell... // Set up the cell MyAppAppDelegate *appDelegate = (MyAppAppDelegate *)[[UIApplication sharedApplication] delegate]; if (tableView == radios_tv) { //radio_tv is an IBOutleet UITableView sqlClass *aRadio = (sqlClass *)[appDelegate.array_radios objectAtIndex:indexPath.row]; [cell setText:aRadio.r_name]; return cell; } if (tableView == presets_tv) { //preset_tv is an IBOutlet UITableView } } 

和嘿vikingsegundo,现在我需要删除我的TableViewController类的单元格,我该怎么做? 我解释一下,这里是我的代码:

 - (void)tableView:(UITableView *)tv commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if(editingStyle == UITableViewCellEditingStyleDelete) { //Get the object to delete from the array. Coffee *coffeeObj = [appDelegate.coffeeArray objectAtIndex:indexPath.row]; [appDelegate removeCoffee:coffeeObj]; //Delete the object from the table. [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; } } 

既然我们把不同的pipe制员放在一起,那么我们应该怎么做呢? 我应该把tableViewController,而不是“自我”?

 //Delete the object from the table. [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 

IMO最干净的解决scheme将是每个tableview有一个控制器。

radios_tv会调用它自己的委托方法,而presets_tv调用它自己的方法。

编辑

如果你在n个tableview中使用一个控制器,你将不得不在许多地方使用if-statemenst

  • – numberOfSectionsInTableView:
  • – tableView:numberOfRowsInSection:
  • – tableView:titleForHeaderInSection:

基本上在你需要实现的所有UITableViewDatasource-Protocol方法中。

所以,如果你需要改变一些东西,你必须在很多地方改变它。

如果您为一个tableview使用一个控制器类,则根本不需要检查。

  1. 为每个tableview编写一个控制器类,使其符合UITableViewDatasource协议
    • 实施您将需要的协议方法。 至less
      • – numberOfSectionsInTableView:
      • – tableView:numberOfRowsInSection:
      • – tableView:cellForRowAtIndexPath:
  2. 调用-setDataSource:用于每个tableview到右侧控制器类的对象

我认为,这是在WWDC 2010的video之一 。 我不确定,但我想这是iPhone OS的Session 116 – Model-View-Controller

编辑

我写了一个示例代码: http : //github.com/vikingosegundo/my-programming-examples

在一个视图控制器,如果你必须使用两个表,那么你可以设置IBOutlet到两个表或分配不同的标签,所以当你使用下面的cellForRowAtIndexPath你可以在两个表中区分如下

 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCellStyle style =UITableViewCellStyleSubtitle; static NSString *MyIdentifier = @"MyIdentifier"; DataListCell *cell = (DataListCell*)[tableView dequeueReusableCellWithIdentifier:MyIdentifier]; cell = [[DataListCell alloc] initWithStyle:style reuseIdentifier:MyIdentifier]; cell.selectionStyle=UITableViewCellSelectionStyleNone; if(tableView==tblLanguage)//tblLanguage IBOutlet for first table { if ((selectedIndexPath != nil) && (selectedIndexPath.row == indexPath.row)) { UIImageView *imgView=[[UIImageView alloc]initWithFrame:CGRectMake(320-30, 9, 22, 15)]; imgView.image=[UIImage imageNamed:@"btn_Expand.png"]; [cell addSubview:imgView]; tblSongs.hidden=NO; tblSongs.frame=CGRectMake(0,42, 320, ([arrSongListForSpecificLanguage count]*40)); [cell addSubview:tblSongs]; } else { UIImageView *imgView=[[UIImageView alloc]initWithFrame:CGRectMake(320-30, 9, 16, 22)]; imgView.image=[UIImage imageNamed:@"btn_Collaps.png"]; [cell addSubview:imgView]; } cell.lblCustomerName.textColor=[UIColor blackColor]; cell.lblCustomerName.font=[UIFont boldSystemFontOfSize:16]; //set the first label which is always a NamesArray object [cell setcustomerName:[objAppDelegate.viewController.arrLanguage objectAtIndex:indexPath.row]]; } else //for other table { ParseData *objParse; objParse=[arrSongListForSpecificLanguage objectAtIndex:indexPath.row]; cell.lblCustomerName.textColor=[UIColor blackColor]; cell.lblCustomerName.frame=CGRectMake(cell.lblCustomerName.frame.origin.x, cell.lblCustomerName.frame.origin.y, 310, cell.lblCustomerName.frame.size.height); //set the first label which is always a NamesArray object [cell setcustomerName:objParse.track]; } return cell; } } 

你也可以在if语句中使用相同的标签if(tableView.tag == 1)// tblLanguage tag = 1

类似的if语句用于表的其他委托和数据源方法