NSMutableArray *objects
包含一些我用来显示表格视图单元格内容的对象。如果objects.count
为 0,我想在以下情况下启用表格视图的编辑模式viewDidLoad
:
- (void)viewDidLoad {
// If there is no content to present enable editing mode
if (self.objects.count == 0) [self setEditing:YES animated:YES];
}
这会切换self.editButtonItem
并在我的表格视图中插入一个新行(说明“点击此处添加新元素”):
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
[super setEditing:editing animated:animated];
[self.tableView setEditing:editing animated:animated];
if (self.editing) {
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:[self.objects count] inSection:0]]
withRowAnimation:UITableViewRowAnimationAutomatic];
}
else {
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:[self.objects count] inSection:0]]
withRowAnimation:UITableViewRowAnimationAutomatic];
}
}
不幸的是,此设置会导致崩溃:
*** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit/UIKit-1914.85/UITableView.m:833
不能以编程方式切换编辑模式吗?如果用户触摸self.editButtonItem
一切正常 - 据我所知,这个 editButton 和我在viewDidLoad
.
我究竟做错了什么?