2009-10-02 69 views
4

我有一个UITableView带有单节和'n'行(从NSMutableArraymyTableArray填充)。当进入编辑模式,我想实现以下目标:在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再次
的3个电话

我可以理解编辑样式需要重新更正为插入一个新行的结果,但我不明白为什么在最后5次调用中,第0行设置了两次,为什么第1行仍然设法拥有风格不正确。

有没有人有任何见解?

回答

0

尝试关闭插入的动画并查看被调用的次数。

您在这里得到的问题是,当行0是现有的预插入行时,将调用super,然后您将使用动画执行插入操作,这会导致调用editingStyle ..方法。

以下建议很可怕,但您也可以尝试在之前编辑,然后进行行插入/删除操作,然后再调用super。这样正确的行数就位。

更大的问题是......你是肯定你想插入行是最好的吗?这是一个非标准的地方,除非你有一个很好的理由违背了UI准则,否则你可能想要考虑改造你的UI来将插入式风格的行放在底部....

+0

当你说它是非标准的,在顶部插入一行时,你指的是哪个UI指南? iPhone人机界面指南? – 2009-10-08 12:05:32

+0

是的。我将补充说,这是一个事实上的标准,将插入式编辑行(带有绿色加号)作为您允许添加的每个部分中的最后一行。 也就是说,除非你有很好的理由在别处插入。 – 2009-10-25 07:51:20

+0

这是一种烦人的,必须隐藏最常用的功能,在可能长列表的底部。我想总能在编辑启用时滚动到底部,但这可能会打开另一罐动画蠕虫。 – spstanley 2012-12-25 18:07:43

6

我知道这已经过去了一年,但我Google搜索并偶然发现了这个,所以我们来回答这个问题。

以下似乎工作。 TableViewController在它有机会知道行被插入顶部之前请求编辑样式。所以我们应该跟踪我们是否在改变编辑模式和插入之间。

- (void)setEditing:(BOOL)editing animated:(BOOL)animated { 
    automaticEditControlsDidShow = NO; 
    [super setEditing:editing animated:animated]; 
    NSArray *addRow = [NSArray arrayWithObjects:[NSIndexPath indexPathForRow:0 inSection:0],nil]; 
    if (editing) { 
     automaticEditControlsDidShow = YES; 
     [self.tableView insertRowsAtIndexPaths:addRow withRowAnimation:UITableViewRowAnimationLeft]; 
    } else { 
     [self.tableView deleteRowsAtIndexPaths:addRow withRowAnimation:UITableViewRowAnimationLeft]; 
    } 
} 

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { 
    int row = indexPath.row; 

    if (self.editing && row == 0) { 
     if (automaticEditControlsDidShow) 
      return UITableViewCellEditingStyleInsert; 
     return UITableViewCellEditingStyleDelete; 
    } 
    return UITableViewCellEditingStyleDelete; 
} 
+0

编辑意味着插入和不编辑意味着删除? – andrewz 2016-03-10 19:20:08

+0

@andrewz,这是正确的。 – Philosophistry 2016-03-12 00:30:46

相关问题