我需要一个示例或解释如何填充同一 View 上的 2 个 TableView 。我需要了解“cellForRowAtIndexPath”方法,有人可以给我提供一个代码应该如何编写的示例吗?
我的意思是如何识别哪个表格 View ?
谢谢
下面是我的 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];
}
}
既然我们放了不同的controller,那么这条线应该怎么走呢?我应该放 tableViewController 而不是“self”吗?
//Delete the object from the table.
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
最佳答案
IMO 最简洁的解决方案是为每个 tableview 配备一个 Controller 。
radios_tv
会调用它自己的委托(delegate)方法,而 presets_tv
会调用它自己的方法。
编辑
如果你对 n 个 tableview 使用一个 Controller ,你将不得不在很多地方使用 if-statementst, 在
– numberOfSectionsInTableView:
– tableView:numberOfRowsInSection:
– tableView:titleForHeaderInSection:
- …
基本上在您需要实现的所有 UITableViewDatasource-Protocol 方法中。
所以如果你需要改变一些东西,你必须在很多地方改变它。
如果您对一个 TableView 使用一个 Controller 类,则根本不需要检查。
- 为每个tableview写一个controller类,使其符合
UITableViewDatasource
协议(protocol)- 实现您需要的协议(protocol)方法。至少
– numberOfSectionsInTableView:
,– tableView:numberOfRowsInSection:
,– tableView:cellForRowAtIndexPath:
- 实现您需要的协议(protocol)方法。至少
- 调用
-setDataSource:
为每个 tableview 调用正确的 Controller 类的对象
我认为,它显示在 WWDC 2010 videos 之一中.我不确定,但我猜是第 116 节课 - iPhone 操作系统的模型- View - Controller 。
编辑
我写了一个示例代码:http://github.com/vikingosegundo/my-programming-examples
关于objective-c - 2 单个 View 上的 TableView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3489238/