NSMutableArray *objects holds some objects which I use to display the content of my table view cells. If objects.count is 0 I would like to enable the editing mode of my table view when viewDidLoad:
- (void)viewDidLoad {
// If there is no content to present enable editing mode
if (self.objects.count == 0) [self setEditing:YES animated:YES];
}
This toggles the self.editButtonItem and inserts a new row (stating “Tap here to add a new element”) into my table view:
- (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];
}
}
Unfortunately this setup results in a crash:
*** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit/UIKit-1914.85/UITableView.m:833
Is it not possible to toggle the editing mode programmatically? If the user touches the self.editButtonItem everything works fine – and as far as I know, this editButton does the same I’m doing in viewDidLoad.
What am I doing wrong?
i think there are 2 different questions here.
one, it is possible to perform
setEditing:animated:programmatically.but i don’t think that’s what you really want to try to do here. the editing mode is for the user to manually edit the table, and present the little red button on the left, and possibly the little movement indicator on the right if you have those settings set.
the better thing to do is when you find your objects has changed, perform a
[self.tableView reloadData];, and just make sure that yourUITableViewDataSourceprotocol methods implemented do the right thing. this will include the implementation oftableView:numberOfRowsInSection:(and possibly alsonumberOfSections) and alsotableView:cellForRowAtIndexPath:. this will cause the items to appear in the tableView asobjectschanges.