NSMutableArray *objects
包含一些对象,我用它来显示我的表视图单元格的内容。如果objects.count
是0,我想使我的表视图的编辑模式时viewDidLoad
:表视图:setEditing以编程方式
- (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
是没可能切换编辑MOD通过编程?如果用户触摸self.editButtonItem
一切正常 - 并且据我所知,这个editButton与我在viewDidLoad
中做的一样。
我在做什么错?
感谢您的回答。我认为这是一个误解:我确实希望我的tableview在我的“点击此处添加一个新元素”行旁边显示红色小按钮。所以它看起来应用程序已经点击了用户的“编辑”按钮。在点击“编辑”按钮之前,视图是空的,因为tableview没有数据要显示。所以用户在任何情况下都会点击“编辑”按钮。为了保存这一步,我想以编程方式启用编辑模式。 – Patrick 2012-07-21 18:17:26
如果这就是你想要实现它,那么仍然做所有我推荐的东西,使tableView与对象的变化一起出现,并使用setEditing实现:animated:仅在调用super和self.tableView时才实现,并且不要在setEditing:animated:调用中执行插入和删除操作。意识到对于用户删除的任何内容,您还必须实现tableView:commitEditingStyleForRowAtIndexPath:。如果您在编辑时实施了添加项目的方法,情况也会如此。 – 2012-07-21 18:44:59
您的注意事项不要在'setEditing:animated:'内执行插入和删除操作,实际上帮助我解决了这个问题:我现在使用正确的方法来处理这个问题,使用我的'tableView:numberOfRowsInSection: :animated:'我正在重新载入我的表格视图的内容 - 就像您在原始答案中推荐的一样。再次感谢! – Patrick 2012-07-22 10:45:12