This is the first time I ask a question here, but I have to say this site has been a tremendous help for me over the last couple months (iphone-dev-wise), and I thank you fo
it shouldn't be necessary to call both deleteRowsAtIndexPaths:withAnimation:
and reloadData
on tableView.
the problem you are seeing is the problem i see manifesting in the call to deleteRowsAtIndexPaths:withAnimation:
, and thus for you, it's never getting to reloadData
anyway.
a simpler solution to having to create new cells, manage them, deal with all of the places where a different cell has to be dealt with, is to use reloadSections:withAnimation:
when dealing with a section that is going from 0 to 1 row or 1 to 0 rows.
i.e. replace the following lines of code:
if(indexPath.section == 0)
{ [firstSectionArray removeObjectAtIndex:indexPath.row]; }
else
{ [secondSectionArray removeObjectAtIndex:indexPath.row]; }
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationBottom];
with the following
BOOL useReloadSection;
if (indexPath.section == 0)
{
[firstSectionArray removeObjectAtIndex:indexPath.row];
useReloadSection = 0 == firstSectionArray.count;
}
else
{
[secondSectionArray removeObjectAtIndex:indexPath.row];
useReloadSection = 0 == secondSectionArray.count;
}
if (useReloadSection)
[tableView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section]
withRowAnimation:UITableViewRowAnimationBottom];
else
[tableView deleteRowsAtIndexPaths:@[indexPath]
withRowAnimation:UITableViewRowAnimationBottom];
I was experiencing the same crash, looks like it's a bug in the OS.
My specific use case pertains to a UITableView with collapsable / expandable sections. I need section dividers (this is what I was using the section footer for) but not row dividers (can't simply use the cell separator).
I wasn't able to resolve the issue by adjusting the height of the section footer. But switching to using a section header did the trick, the bug/crash is no longer occurring.
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
CGRect frame = (CGRect){0, 0, self.view.bounds.size.width, 1};
UIView *view = [[UIView alloc] initWithFrame:frame];
view.backgroundColor = [UIColor commentReplyBackground];
return view;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 1.0f;
}
It looks like what it happening is that internally Apple's UITableView
code is assuming that when you delete a row, the first section of your view will get shorter. By making the section header footer suddenly get taller to compensate for the last row, you appear to be confusing it.
Two ideas for you to try:
1. try making the optional section footer a pixel or two smaller than the table cell that's going away, so that the animation code gets to do a pixel or two of animation
2. instead of deleting the only row of your table, when there are no "real" data rows let the table still have numberOfRowsInSection
return 1 and make a fake "no data" cell rather than using a table footer for the "no data" case.
This is one of those cases where you just have to accept that Apple has written half of your program for you and you have to conform to some choices your co-author has made, whether you like them or not.
For those of you who still need the section footer, you can also try changing the TableView style to UITableViewStyleGrouped. This seems to bypass the issue. In order to get the same visual appearance, you just have to set all your other footers and headers to CGFLOAT_MIN.
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { if (section != OTHER_SECTION_INDEX) { return nil; } //your code here } - (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { if (section != OTHER_SECTION_INDEX) { return 0; } //your code }