作为进入编辑模式的一部分,在UITableView顶部插入新行时编辑样式的问题

Ben*_*ver 4 iphone uitableview

我有一个UITableView与单个部分和"n"的行(从填充NSMutableArray,myTableArray).进入编辑模式时,我想实现以下目标:

  • 设置"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;
}
Run Code Online (Sandbox Code Playgroud)

运行此代码并按下"编辑"按钮时,会在原始"n"行上方插入一个新行,但是,第二行除了第一行外,还具有UITableViewCellEditingStyleInsert编辑样式.例如,如果'n'为3,则在按下编辑按钮后,表格将显示:

  • 第0行:插入编辑样式
  • 第1行:插入编辑样式(应删除)
  • 第2行:删除编辑样式
  • 第3行:删除编辑样式

经过进一步调查,我注意到如果'n'为3且按下了编辑按钮,则该方法tableView:editingStyleForRowAtIndexPath:总共被调用8次:

  • [super setEditing:flag animated:animated] 导致每个原始行0,1和2的3个调用
  • 随后的[tableView insertRowsAtIndexPaths:topIndexPath withRowAnimation:UITableViewRowAnimationBottom]结果是5次调用行0,1,2,3,然后再次调用0

我可以理解,由于插入新行,需要重新校正编辑样式,但是我不明白为什么在最后5次调用中,第0行设置了两次,为什么第1行仍然设法使用不正确的样式.

有没有人有任何见解?

phi*_*pkd 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;
}
Run Code Online (Sandbox Code Playgroud)