tableview refresh crush Thread1:signal SIGABRT - ios

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.

Related

How to pass in section value while storing indexPath in NSMutableArray

I am trying to store indexPath in NSMutableArray
[checkBoxIndexArray addObject:[NSIndexPath indexPathForRow:indexPath.row inSection:?]];
- (void)reloadItemsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths
{
cell.checkBox.image = [UIImage imageNamed:#"check"];
[self.collectionView insertItemsAtIndexPaths:checkBoxIndexArray];
}
but i don't know on what value shall i put for inSection. I tried entering 0,1 but still it's not working. I am getting following error
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of items in section 0. The number of items contained in an existing section after the update (2) must be equal to the number of items contained in that section before the update (2), plus or minus the number of items inserted or deleted from that section (1 inserted, 0 deleted) and plus or minus the number of items moved into or out of that section (0 moved in, 0 moved out).
i am getting this error for following code
[self.collectionView insertItemsAtIndexPaths:checkBoxIndexArray];
Please tell me what value shall i put for inSection.
you should use
[checkBoxIndexArray addObject: indexPath];
NSIndexPath *indexPath=[Your_Table_Name indexPathForSelectedRow];
[checkBoxIndexArray addObject: indexPath];
Doing
[checkBoxIndexArray addObject:[NSIndexPath indexPathForRow:indexPath.row inSection:?]]
is same as doing this
[checkBoxIndexArray addObject:[NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section]]
OR Doing this
[checkBoxIndexArray addObject:indexPath]
Add the indexPath directly, and later on access the rows or section values as you please.

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

Select Saved Checkmarked Rows on viewdidload

I have a settings page with a long list with checkmark accesories. Users can select as many rows as they want and I have them saving in NSUserDefaults. I want their previous selections to be selected next time they open settings so I've tried this:
NSArray *array = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:#"choices"]];
if (array.count !=0) {
NSLog(#"not empt");
for (id obj in array) {
NSIndexPath *path = [NSIndexPath indexPathWithIndex:obj];
[self.tableView selectRowAtIndexPath:path animated:NO scrollPosition:UITableViewScrollPositionTop];
}
}
But the app crashes with this error every time:
erminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid index path for use with UITableView. Index paths passed to table view must contain exactly two indices specifying the section and row. Please use the category on NSIndexPath in UITableView.h if possible.
What am I doing wrong? Thanks
The error is quite explanatory. NSIndexPaths in UIKit require two indexes one for the section and one for the row. It's explained here
Assuming you only have one section in your table view:
for (id obj in array) {
NSIndexPath *path = [NSIndexPath indexPathForRow:obj inSection:0];
[self.tableView selectRowAtIndexPath:path animated:NO scrollPosition:UITableViewScrollPositionTop];
}

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