I got a strange problem with my UITableView: I use reloadRowsAtIndexPaths:withRowAnimation:
to reload some specific rows, but the app crashes with an seemingly unrelated exception: NSInternalInconsistencyException - Attempt to delete more rows than exist in section.
My code looks like follows:
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationFade];
When I replace that reloadRowsAtIndexPaths:withRowAnimation:
message with a simple reloadData
, it works perfectly.
Any ideas?
The app crashes because you have made some changes to your tableView. Either you have added or deleted some rows to the tableView. Hence when the view controller asks your model controller class for data, there is a mismatch in the indexPaths. Since the indexPaths have changed after modification.
So either you simply remove the call
or replace it with
Calling reloadData checks your number of sections, number of rows in each section and then reloads the whole thing.
You should check cell visibility before reload. Here is Swift 3 code:
I had the same issue. In my case; it was happening only if another view controller pop/pushed over existing table view controller and then[self.tableView reloadRowsAtIndexPaths] function is called.
reloadRowsAtIndexPaths call was hiding/showing different rows in a table view which is having over 30, visually complex, rows. As i try to fix the issue i found that if i slightly scroll the table view app wasn't crashing. Also it wasn't crashing if i don't hide a cell (by returning 0 as height)
To resolve the issue, i simply changed the "(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath" function and returned at least 0.01 as row height.
it solved the issue for me.
I had this problem which was being caused by a block calling reloadRowsAtIndexPaths:withRowAnimation: and a parallel thread calling reloadData. The crash was due to reloadRowsAtIndexPaths:withRowAnimation finding an empty table even though I'd sanity checked numberOfRowsInSection & numberOfSections.
I took the attitude that I don't really care if it causes an exception. A visual corruption I could live with as a user of the App than have the whole app crash out.
Here's my solution to this which I'm happy to share and would welcome constructive criticism. If there's a better solution I'm keen to hear it?
The problem is that you, probably, have changed size of your table. For example, you have add or remove some source data for table view.
In that case, when you call
reloadData
your table is completely reloaded including sizes of sections and number of that sections.But when you call
reloadRowsAtIndexPaths:withRowAnimation:
that parameters of your table view are not reloaded. That causes next problem: when you are trying to reload some cell table check the size of table view and sees that it has been changed. That results in a crash. This method can be used only when you wan't to reload view of the cell (for example, label has changed or you want to change its size).Now if you want to remove/add cells from/to table view you should use next approach:
beginUpdates
ofUITableView
- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
endUpdates
ofUITableView
Hope it will be helpful
I think the following code might work: