我有一个UITableView
带有单节和'n'行(从NSMutableArray
,myTableArray
填充)。当进入编辑模式,我想实现以下目标:在UITableView顶部插入新行作为进入编辑模式的一部分时编辑样式的问题
- 设置了“N”行有
UITableViewCellEditingStyleDelete
编辑样式 - 插入新行,原来的“N”以上,并设置它有
UITableViewCellEditingStyleInsert
编辑样式
我UITableViewController
子类实现如下:
- (void)setEditing:(BOOL)flag animated:(BOOL)animated
{
[super setEditing:flag animated:animated];
UITableView *tableView = (UITableView *)self.view;
NSArray *topIndexPath = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]];
if (self.editing == YES)
[tableView insertRowsAtIndexPaths:topIndexPath withRowAnimation:UITableViewRowAnimationBottom];
else
[tableView deleteRowsAtIndexPaths:topIndexPath withRowAnimation:UITableViewRowAnimationBottom];
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0)
return UITableViewCellEditingStyleInsert;
else
return UITableViewCellEditingStyleDelete;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (self.editing == YES)
return [myTableArray count] + 1;
else
return [myTableArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// ... boiler-plate dequeueReusableCellWithIdentifier code, etc ...
if (self.editing == YES)
{
if (indexPath.row == 0)
cell.textLabel.text = @"My new row";
else
cell.textLabel.text = [myTableArray objectAtIndex:(indexPath.row - 1)];
}
else
{
cell.textLabel.text = [myTableArray objectAtIndex:indexPath.row];
}
return cell;
}
当这个代码运行,而t他按下编辑按钮,在原来的'n'行之上插入一个新行,但除第一行之外,第二行具有UITableViewCellEditingStyleInsert
编辑样式。例如,如果“n”为3,后编辑按钮被按下表显示:
- 行0:插入编辑风格
- 行1:插入编辑样式(应删除)
- 行2:删除的剪辑风格
- 第3行:删除剪辑风格
经过进一步调查,我发现,如果“n”为3,编辑按钮被按下时,方法tableView:editingStyleForRowAtIndexPath:
被称为共8次:
[super setEditing:flag animated:animated]
导致用于每个原始行0,1和2- 随后
[tableView insertRowsAtIndexPaths:topIndexPath withRowAnimation:UITableViewRowAnimationBottom]
结果在5个呼唤行0,1,2,3,然后0再次
我可以理解编辑样式需要重新更正为插入一个新行的结果,但我不明白为什么在最后5次调用中,第0行设置了两次,为什么第1行仍然设法拥有风格不正确。
有没有人有任何见解?
当你说它是非标准的,在顶部插入一行时,你指的是哪个UI指南? iPhone人机界面指南? – 2009-10-08 12:05:32
是的。我将补充说,这是一个事实上的标准,将插入式编辑行(带有绿色加号)作为您允许添加的每个部分中的最后一行。 也就是说,除非你有很好的理由在别处插入。 – 2009-10-25 07:51:20
这是一种烦人的,必须隐藏最常用的功能,在可能长列表的底部。我想总能在编辑启用时滚动到底部,但这可能会打开另一罐动画蠕虫。 – spstanley 2012-12-25 18:07:43