UITableView行动

在24小时30分多的WWDC 11video“会议125 – UITableCiew变更,技巧和窍门”中,Luke Hiesterman先生正在给出一个演示,当选中一个单元格时,会在表格视图中添加一个单元格。

视频截图

我想添加到我的IOS应用程序的function,但我不知道如何做到这一点。

演示video中没有显示一些代码。 并没有可下载的演示源。

谁能帮我吗?

编辑:

我可以在所选行下面添加一个新行,但是它是另一个自定义单元格。

(我有你可以接受的合同清单)

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return ((indexPath.row >= [_contracts count]) ? 40 : 60); } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [_contracts count] + ((_addedRow) ? 1 : 0); } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSString *reuseID = @"contract_cell"; ContractTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseID]; if(!cell) { // Load the top-level objects from the custom cell XIB. NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"ContractTableViewCell" owner:self options:nil]; // Grab a pointer to the first object (presumably the custom cell, as that's all the XIB should contain). cell = [topLevelObjects objectAtIndex:0]; [cell setBackgroundView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"contract_cell.png"]]]; } NSLog(@"row?: %d", indexPath.row); //I thought this would work... not. if((indexPath.row >= [_contracts count])) { [cell.label setText:@"new"]; } else { Contract *contract = [_contracts objectAtIndex:indexPath.row]; [cell.label setText:@"test"]; } return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if(!_addedRow) { _addedRow = YES; [tableView deselectRowAtIndexPath:indexPath animated:NO]; [_tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:indexPath.row+1 inSection:indexPath.section]] withRowAnimation:UITableViewRowAnimationAutomatic]; } } - (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath { return [NSIndexPath indexPathForRow:indexPath.row+1 inSection:indexPath.section]; } 

例如,我按第一行。 在第一个下面添加一行。 但是现在为行2调用了cellForRowAtIndexPath ..(需要是新的自定义)

我如何检查它是否是新的?

您需要记住哪些行被选中才能在正确的索引处显示添加的行。 目前,您始终认为添加的行是最后一行,因为您在tableView:cellForRowAtIndexPath:configurationtableView:cellForRowAtIndexPath:只有在索引大于您的合同数量的情况下。

假设你有五个合同,第三个合同被选中。 if((indexPath.row >= [_contracts count]))只对最后一行为真,但实际上这个条件对于第四行是真的,所以应该是if (indexPath.row == selectedRowIndex + 1) (您需要将选定的行索引存储在某个实例variables中)。

这个答案应该可以帮助你。 它告诉你如何把你的UITableViewController进入“更新模式”,然后让你插入新的行(即使有animation): UITextField在UITableViewCell中 – 添加新的单元格