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

标签 iphone uitableview

我有一个 UITableView,其中包含一个部分和“n”行(由 NSMutableArraymyTableArray 填充)。当进入编辑模式时,我想实现以下目标:

  • 将“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;
}

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

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

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

  • [super setEditing:flaganimated:animated] 导致对原始行 0、1 和 2 中的每一行进行 3 次调用
  • 后续的 [tableView insertRowsAtIndexPaths:topIndexPath withRowAnimation:UITableViewRowAnimationBottom] 导致对第 0、1、2、3 行进行 5 次调用,然后再次调用 0

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

有人有见解吗?

最佳答案

我知道已经过去一年了,但我在 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;
}

关于iphone - 作为进入编辑模式的一部分,在 UITableView 顶部插入新行时编辑样式出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1508066/

相关文章:

iphone - 如何在 Cocos2d 中创建 AI?

iphone - 通过耳机插孔输入iOS线路(适配器?)

ios - 在tableView中commitEditingStyle时,imageView高度出现大溢出?

ios - UITableView 始终显示 4 个单元格

ios - 如何将 UITableViewCell 显示为选中状态并仍然接收 didDeselectRowAtIndexPath

iOS - 即使使用 NavController,Push Segue 也无法正常工作

ios - UIView 内容模式在覆盖 drawRect : 时表现不同

iphone - 为 iPhone 编写 LaunchDaemon

ios - 无法将类型 'UITableViewCell' (0x10c111c68) 的值转换为子类

ios - 通过用户 ID 或某个唯一 ID :Issue 获取 PFInstallation