UITableView's "attempt to delete section" assertion failure - ios

I'm trying to update UITableView with the following code:
NSMutableIndexSet *sectionsToDelete = [NSMutableIndexSet indexSet];
NSMutableIndexSet *sectionsToInsert = [NSMutableIndexSet indexSet];
NSMutableIndexSet *sectionsToReload = [NSMutableIndexSet indexSet];
/* ... */
[[self tableView] beginUpdates];
if ([sectionsToReload count]) {
DBGLogObject(sectionsToReload);
[[self tableView] reloadSections:sectionsToReload withRowAnimation:animation];
}
if ([sectionsToDelete count]) {
DBGLogObject(sectionsToDelete);
[[self tableView] deleteSections:sectionsToDelete withRowAnimation:animation];
}
if ([sectionsToInsert count]) {
DBGLogObject(sectionsToInsert);
[[self tableView] insertSections:sectionsToInsert withRowAnimation:animation];
}
[[self tableView] endUpdates];
Log:
sectionsToReload = <NSMutableIndexSet: 0x71b19f0>[number of indexes: 2 (in 1 ranges), indexes: (1-2)]
sectionsToInsert = <NSMutableIndexSet: 0x71ac570>[number of indexes: 3 (in 2 ranges), indexes: (0 3-4)]
(Note there's no sectionsToDelete in the log.)
The thing is that even though there's no deletion I get an error:
2012-10-04 19:21:16.769 Syntax[903:c07] *** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-2372/UITableView.m:826
2012-10-04 19:24:51.655 Syntax[903:c07] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'attempt to delete section 2, but there are only 2 sections before the update'
Any ideas?

Actually, now I have duplicated your error -- if your data only has 2 sections before this update (which is what's implied by the error message), then you can't reload sections 1 and 2. Did you mean to update 0 and 1? I'm guessing that when the reload happens, it must first delete the old data and add the new, so that's where the delete error message is coming from.

Did "Clean Build Folder" and now it works like expected. I have no idea why.

you should change your datasource, if you delete row or section you should change the number of rows or section in tableView

Related

tableview refresh crush Thread1:signal SIGABRT

My app is a IM application, when the application into the background and once again back to the foreground will crush.
This is my part of the code。
-(void)uiAddChatroomMessages:(NSArray*)messages{
NSMutableArray *indexPaths = [[NSMutableArray alloc]init];
int i = (short)self.messageArray.count ;
for (RTIMMessage *msg in messages) {
[self.messageArray addObject:msg];
}
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
[indexPaths addObject:indexPath];
[self.chatMessageTableView beginUpdates];
[self.chatMessageTableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
[self.chatMessageTableView endUpdates];
[self.chatMessageTableView reloadData];
}
Run to this code "[self.chatMessageTableView endUpdates]",It crush and prompt "Thread1:signal SIGABRT".
2016-08-24 15:49:18.500 RTIM_iOS_Demo[1834:1326398] * Assertion
failure in -[UITableView _endCellAnimationsWithContext:],
/BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit/UIKit-3512.60.12/UITableView.m:1716
2016-08-24 15:49:18.590 RTIM_iOS_Demo[1834:1326398] * Terminating
app due to uncaught exception 'NSInternalInconsistencyException',
reason: 'Invalid update: invalid number of rows in section 0. The
number of rows contained in an existing section after the update (62)
must be equal to the number of rows contained in that section before
the update (57), plus or minus the number of rows inserted or deleted
from that section (1 inserted, 0 deleted) and plus or minus the number
of rows moved into or out of that section (0 moved in, 0 moved out).'
*** First throw call stack: (0x18238adb0 0x1819eff80 0x18238ac80 0x182d10154 0x1876e1808 0x100072ccc 0x1000728b0 0x100095244
0x10009468c 0x100077d20 0x100241a7c 0x100241a3c 0x1002474e4
0x182340d50 0x18233ebb8 0x182268c50 0x183b50088 0x187552088
0x1000a5340 0x181e068b8) libc++abi.dylib: terminating with uncaught
exception of type NSException
In your method you added to self.messageArray some new objects but added just one row. Modify your code to.
-(void)uiAddChatroomMessages:(NSArray*)messages{
NSMutableArray *indexPaths = [NSMutableArray array];
NSUInteger i = self.messageArray.count;
for (RTIMMessage *msg in messages) {
[self.messageArray addObject:msg];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
[indexPaths addObject:indexPath];
i ++;
}
[self.chatMessageTableView beginUpdates];
[self.chatMessageTableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
[self.chatMessageTableView endUpdates];
[self.chatMessageTableView reloadData];
}
As per the stack trace it seems like your index path is wrong. You are using array count as row index, try with (array count-1), hope it will work.
After inserting you the number returned by numberOfRowsInSection must match with the new expected value. You might be returning a constant or a number of elements of an array which hasn't changed.

can't delete cell in searchResultsTableView

I can't delete cell in the resultant table in normal everything is OK and after search gives out an error
Assertion failure in -[UISearchResultsTableView
_endCellAnimationsWithContext:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit/UIKit-3512.29.5/UITableView.m:1720
2015-12-14 09:29:04.907 Unirenter[552:178776] *** Terminating app due
to uncaught exception 'NSInternalInconsistencyException', reason:
'Invalid update: invalid number of rows in section 0. The number of
rows contained in an existing section after the update (1) must be
equal to the number of rows contained in that section before the
update (1), plus or minus the number of rows inserted or deleted from
that section (0 inserted, 1 deleted) and plus or minus the number of
rows moved into or out of that section (0 moved in, 0 moved out).'
*** First throw call stack: (0x220e185b 0x337a2dff 0x220e1731 0x22e72ddb 0x26408269 0x264218c9 0x264217a3 0x8ab6b 0x26555d97
0x26692ba5 0x2624d5e1 0x2624d571 0x26235487 0x2624cec1 0x26206fe7
0x26245c89 0x2624543b 0x262162f1 0x26214937 0x220a47c7 0x220a43b7
0x220a271f 0x21ff50d9 0x21ff4ecd 0x2b36aaf9 0x2627e2dd 0x8f031
0x33ef0873) libc++abi.dylib: terminating with uncaught exception of
type NSException
here a code of deleting
UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:#"Delete" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath){
if (tableView == self.tableView) {
[self deleteMessage:[_id_message objectAtIndex:indexPath.row]];
[_id_message removeObjectAtIndex:indexPath.row];//or something similar to this based on your data source array structure
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
else
{
NSLog(#"Удаляем id -%d ",[self TitleWithId:[_filteredCandyArray objectAtIndex:indexPath.row]]);
// [_id_message removeObjectAtIndex:[self TitleWithId:[_filteredCandyArray objectAtIndex:indexPath.row]]];
[self.searchDisplayController.searchResultsTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
This code is in a method tableView editActionsForRowAtIndexPath
I understood what business. In the simple table I deleted at first an element and then updated and in the retrieval isn't present. I added a method [_filteredCandyArray removeObjectAtIndex:indexPath.row]; and everything earned
You delete the item from it's data source you send deleteRowsAtIndexPaths:withRowAnimation: to it and it should work fine. The same rules apply as to any tableView, if you modify the data source and want to incrementally update the table view make sure you keep the changes consistent between the data source and the table structure. Thnx

insertRowsatIndexPath exception iOS

* Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'attempt to insert row 0 into section 0, but there are only 0 rows in section 0 after the update'
I realize the issue here is that I am trying to add a cell to the tableView but it doesn't want it isn't adding one and I can't figure out why.
- (IBAction)addNewIngredient: (id) sender
{
NSString *newIngredient = [recipe createIngredient];
[[recipe ingredients]addObject:newIngredient];
//figure out where that item is in the array
int lastRow = [[recipe ingredients]indexOfObject: newIngredient];
NSIndexPath *ip = [NSIndexPath indexPathForRow:lastRow inSection:0];
NSLog(#"Added a new ingredient!");
[[self tableView] insertRowsAtIndexPaths:[NSArray arrayWithObject:ip] withRowAnimation:
UITableViewRowAnimationTop];
}
To clarify what I'm trying to do here:
recipe has a property ingredients which is an array of NSStrings.
I am adding an object to the ingredients array (does this automatically increase the array count or do I have to do it manually?)...
Then I'm figuring out where that item is in the array with "int lastRow = [[recipe ingredients]indexOfObject:newIngredient]"
Then getting an index path to point there. and then inserting that ingredient into the tableView.
The reason for your exception is that when you insert the row, your numberOfRowsInSection will be called again.
At that time, your data needs to be updated so that if you had 0 rows and are inserting one into it, the function returns 1

Deleting Row from TableView and coreData

I have some problem with deleting rows from a tableView which is filled with coreData objects.
Trying to do this:
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath*)indexPath{
if (editingStyle == UITableViewCellEditingStyleDelete) {
[self.tableView beginUpdates];
NSManagedObject *managedObject = [self.fetchedResultsController objectAtIndexPath:indexPath];
NSLog(#"Deleting (%#)", [managedObject valueForKey:#"original_title"]);
[self.managedObjectContext deleteObject:managedObject];
[self.managedObjectContext save:nil];
[self performFetch];
[self.tableView endUpdates];
}
}
So, when I click the "Delete" button, the app crashes with the following Log:
*** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-2872.3/UITableView.m:1254
2013-08-13 18:39:17.624 RR_proto[1076:a0b] *** Terminating app due to uncaught exception
'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of sections.
The number of sections contained in the table view after the update (1) must be equal
to the number of sections contained in the table view before the update (2), plus or minus
the number of sections inserted or deleted (0 inserted, 0 deleted).'
*** First throw call stack:
I also tried to add the following line after [self.managedObjectContext save:nil]:
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
But the app crashes like before.
One more thing, everytime i start the app again after a crash through delete-action, the cell which should be deleted is really gone!
Would be very nice, if somebody can help.
I know there are many questions about similar problems and I tried different things from these, without success.
Thanks!
The table view is updated automatically by the fetched results controller delegate
methods when objects are added, deleted or modified.
Therefore, to remove an object, just delete it from the managed object context.
Don't call beginUpdates, endUpdates, deleteRowsAtIndexPaths or performFetch:
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath*)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
NSManagedObject *managedObject = [self.fetchedResultsController objectAtIndexPath:indexPath];
[self.managedObjectContext deleteObject:managedObject];
[self.managedObjectContext save:nil];
}
}

What does the iOS error "request for rect of invalid section" mean?

Google reveals no posts at all for this error message. I'm getting it in iOS 5 trying to update a UITableView. While the exact code is a bit tortured, this is what I'm doing to the table and the NSMutableArray that has the table data. The calls are made from off of the main thread via performSelectorOnMainThread calls, as indicated in the code snippets below. The NSMutableArray sections is an array of arrays, each representing a section, where these secondary arrays are NSStrings with the text that appears in the table.
From off of the main thread:
[table performSelectorOnMainThread: #selector(beginUpdates) withObject: nil waitUntilDone: YES];
The main code called from another performSelectorOnMainThread:
// Make sure the requested section exists.
if (sections == nil)
sections = [[NSMutableArray alloc] init];
while (section >= [sections count]) {
NSMutableArray *newArray = [[NSMutableArray alloc] init];
[sections addObject: newArray];
[table insertSections: [NSIndexSet indexSetWithIndex: [sections count]]
withRowAnimation: UITableViewRowAnimationNone];
}
// Insert the new row in the section.
NSMutableArray *rowArray = [sections objectAtIndex: section];
if (row > [rowArray count])
row = [rowArray count];
[rowArray insertObject: cellString atIndex: row];
[table insertRowsAtIndexPaths: [NSArray arrayWithObject: [NSIndexPath indexPathForRow: row inSection: section]]
withRowAnimation: UITableViewRowAnimationNone];
Called after the above code completes:
[table performSelectorOnMainThread: #selector(endUpdates) withObject: nil waitUntilDone: YES];
The error is occurring on this call to endUpdates. The exact error message is:
2012-01-04 14:28:10.951 myApp[2183:fb03] *** Assertion failure in -[UITableViewRowData rectForSection:], /SourceCache/UIKit_Sim/UIKit-1912.3/UITableViewRowData.m:1449
2012-01-04 14:28:10.953 myApp[2183:fb03] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'request for rect of invalid section (1)'
*** First throw call stack:
(0x1833052 0x1d94d0a 0x17dba78 0x11472db 0x99c832 0xa1b93b 0xa1b886 0xa1b451 0xa28134 0x8458e1 0x842602 0x84d211 0x84d23f 0x1834e72 0x10d69ef 0x180797f 0x176ab73 0x176a454 0x1769db4 0x1769ccb 0x33b6879 0x33b693e 0x7bea9b 0x1fad 0x1f25)
terminate called throwing an exception
What does this error mean, and what do I need to change to avoid it?
Looks like your code is trying to work with a section of your table that does not exist.
Since NSArrays are zero-indexed, the index value of count will be outside the bounds of the array. You should subtract one from the count when using indexSetWithIndex: here:
[table insertSections: [NSIndexSet indexSetWithIndex: [sections count]-1]
withRowAnimation: UITableViewRowAnimationNone];
Posting as answer in case it happens to somebody else as well.
I found the issue looking at jonkroll's answer, but in my case this happened because I was calling the same method twice and that method removed a row from the table view.
Leaving this here as well as a separate possible cause.
If you are trying to delete rows in the moveRowAtIndexPath: method you will get the same crash. You have to wait until it finishes to do that.

Resources