单个视图上的2个tableview

awl*_*lcs 4 cocoa-touch objective-c uitableview ios

我需要一个示例或解释如何填充在同一视图上的2个表视图.我需要了解"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


    }

}
Run Code Online (Sandbox Code Playgroud)

和嘿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];
    }
}
Run Code Online (Sandbox Code Playgroud)

由于我们放置了不同的控制器,我们应该如何处理这条线?我应该把tableViewController而不是"self"吗?

//Delete the object from the table.
            [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
Run Code Online (Sandbox Code Playgroud)

Vik*_*ica 9

IMO最干净的解决方案是为每个tableview配备一个控制器.

radios_tv将其称为自己的委托方法,同时presets_tv调用它自己的方法.

编辑

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

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

基本上所有UITableViewDatasource协议的方法,你将需要实现.

因此,如果您需要更改某些内容,则必须在许多地方进行更改.

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

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

我想,它已在WWDC 2010视频中播出.我不确定,但我想这是第116节 - 适用于iPhone OS的模型 - 视图 - 控制器.

编辑

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