如何在编辑模式下在 UITableView 中添加额外的单元格?

Zor*_*mic 5 iphone cocoa-touch objective-c uitableview

您知道如何在表格进入编辑模式后在表格视图中显示某些单元格吗?就像编辑联系人时“联系人”iPhone 应用程序所做的那样。

也许我错了,但是在编辑联系人时,它看起来像是使用了分组的​​ UITableView。

我试过这个:

[self.tableView setEditing:YES animated:YES];
[self.tableView insertSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationBottom];
Run Code Online (Sandbox Code Playgroud)

我的表格在不编辑时只有 1 个部分,我想在编辑时添加一个额外的部分(为了简单起见),但是上面对“insertSections”的调用崩溃了(我的所有表格代表都很好地考虑了 1 或 2 个部分)根据 self.editing,我尝试在正常模式下显示两个部分,并且效果很好)

对于“numberOfSectionsInTableView”,我有:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if (!self.editing) return 1;
    return 2;
}
Run Code Online (Sandbox Code Playgroud)

Joo*_*ost 5

你在哪里进入tableView的编辑状态?您应该在 -[UINavigationController setEditing:animated:] 中执行此操作:

- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    [super setEditing:editing animated:animated];
    [self.tableView setEditing:editing animated:animated];

    if(editing){
        [self.tableView insertSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationBottom];
    } else {
        // delete section
    }
}
Run Code Online (Sandbox Code Playgroud)

因此,不是在 tableView 上调用 setEditing,而是在 navigationController (可能self)上调用它,然后控制器将处理编辑模式。

所以控制器有编辑模式,tableView 有。一旦您以编程方式设置 tableView,或者当用户滑动以删除一行时,tableView 将处于编辑模式。在后一种情况下,该控制器处于编辑模式,但的tableView