Iphone UITableView操作行
在24:30的WWDC 11视频“session 125-UITableCiew Changes,Tips,Tricks”中,Luke Hiesterman先生正在演示如何在选中单元格时将单元格添加到表视图中 我想将该功能添加到IOS应用程序中,但我不知道如何实现 有些代码没有显示在演示视频中。而且没有可下载的演示源 有人能帮我吗 编辑: 我可以在所选行下方添加新行,但它是另一个自定义单元格 (我有一份你可以接受的合同清单) 例如,我按下第一行。在第一行下方添加一行。但是现在cellForRowAtIndexPath。。为第2行调用(需要为新自定义)Iphone UITableView操作行,iphone,objective-c,ios,ipad,uitableview,Iphone,Objective C,Ios,Ipad,Uitableview,在24:30的WWDC 11视频“session 125-UITableCiew Changes,Tips,Tricks”中,Luke Hiesterman先生正在演示如何在选中单元格时将单元格添加到表视图中 我想将该功能添加到IOS应用程序中,但我不知道如何实现 有些代码没有显示在演示视频中。而且没有可下载的演示源 有人能帮我吗 编辑: 我可以在所选行下方添加新行,但它是另一个自定义单元格 (我有一份你可以接受的合同清单) 例如,我按下第一行。在第一行下方添加一行。但是现在cellForRo
我如何检查它是否是新的?这个答案应该可以帮助您。它告诉您如何将UITableViewController置于“更新模式”,然后允许您插入新行(即使使用动画):您需要记住选择了哪些行,以便在正确的索引处显示添加的行。当前,您总是假设添加的行是最后一行,因为只有在索引大于合同数量时,您才在
tableView:cellforrowatinexpath:
中配置它
假设您有五份合同,并且选择了第三份
if((indexPath.row>=[\u contracts count])
仅对最后一行为真,但实际上希望此条件对第四行为真,因此它应该是if(indexPath.row==selectedRowIndex+1)
(您需要将所选行索引存储在某个实例变量中)。我得到的结果是成功的一半。问题是在同一单元格上按下时进行检查(因此删除添加的单元格),如果在其他单元格上按下,则必须删除先前添加的单元格,并在当前单元格上添加新的单元格。在插入/删除的开始和结束时,应调用[tableView BeginUpdate]和[tableView EndUpdate]。
- (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];
}