I keep getting the following error when trying to insert a row:
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 2 beyond bounds [0 .. 1]'
*** First throw call stack:
(0x31fe82a3 0x39ccc97f 0x31f33b75 0x8d18f 0x33fee5a9 0x33edb0c5 0x33edb077 0x33edb055 0x33eda90b 0x33edae01 0x33df9421 0x31fbd6cd 0x31fbb9c1 0x31fbbd17 0x31f2eebd 0x31f2ed49 0x35af62eb 0x33e44301 0xa49d 0x3a103b20)
libc++abi.dylib: terminate called throwing an exception
Does anyone know what's going on here?
Here is my code:
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if(editingStyle == UITableViewCellEditingStyleDelete)
{
Caseload *deletedCaseload = [self.caseload objectAtIndex:indexPath.row];
[self.caseload removeObject:deletedCaseload];
[self.caseloadTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
else if
(editingStyle == UITableViewCellEditingStyleInsert)
{
Caseload *copiedEntry = [self.caseload objectAtIndex:indexPath.row];
Caseload *newEntry = [[Caseload alloc]init];
newEntry.name = copiedEntry.name;
newEntry.address = copiedEntry.address;
newEntry.phoneNumber = copiedEntry.phoneNumber;
newEntry.identNumber = copiedEntry.identNumber;
[self.caseload addObject:newEntry];
[self.caseloadTableView insertRowsAtIndexPaths:
[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationRight];
}
}
This types of error generate whenever you try to get object/value of an array at totalIndex + 1 such like for example if your array has 4 value (Make sure index start with 0) and you try to fetch value such like
[Myarray objectAtIndex:4] at that time this type of error occur.
So best why is use breakPoint and find out your problem by help of my suggestion.
The error is "NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 2 beyond bounds [0 .. 1]'". That means you're calling "objectAtIndex:" somewhere with an index of 2, and your NSArray only has entries at index 0 and index 1.
You need to figure out where you are setting the bogus index, which I suspect might start in your "numberOfRowsInSection:" method.
Related
I have pagination where I add new sections to the UITableView in the following way:
/*
Update my datasource by adding required number of new objects to NSMutableArray
*/
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(startIndex, totalNewSections)];
[myTableView beginUpdates];
[myTableView insertSections:indexSet withRowAnimation:UITableViewRowAnimationNone];
[myTableView endUpdates];
The app randomly crashes on line [ordersTableView endUpdates]; with error:
* Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayM objectAtIndex:]: index 11 beyond bounds [0
.. 10]'
Also, I've checked [self numberOfSectionsInTableView:myTableView] is same as startIndex + totalNewSections. So, no error there.
Edit:
I update my NSMutableArray data source accordingly. It has required number of objects.
Here is some of my code
NSArray *selectedRows = [self.playHistoryTableView indexPathsForSelectedRows];
if (selectedRows.count) {
for (NSIndexPath *selectionIndex in selectedRows)
{
OneSery *sery = [self.playHistoryDic objectAtIndex:selectionIndex.row];
[CoreDataManager deleteOneHistoryBySeryId:sery.seryId andVideoId:sery.latestVideo.videoId];
[self.playHistoryDic removeObjectAtIndex:selectionIndex.row];
}
[self.playHistoryTableView deleteRowsAtIndexPaths:selectedRows withRowAnimation:UITableViewRowAnimationAutomatic];
}
When select the cells one at a time, it works well. But when multiselect the cell it gonna to crash like this:
Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
I know what it mean but I have no idea where I am wrong. And I tried, when it had three cells and I select the first and second to delete, it delete the first and third cell. When it had only two cells, I select both of them to delete, it does crash my app.
When I debug it, even the Onesery *sery value is wrong. So, how could selectionIndex is wrong while selectedRows is right?
Great thanks to #Wain it's finally done with those:
NSArray *selectedRows = [self.playHistoryTableView indexPathsForSelectedRows];
NSMutableIndexSet *indicesOfItemsToDelete = [[NSMutableIndexSet alloc] init];
if (selectedRows.count) {
for (NSIndexPath *selectionIndex in selectedRows)
{
OneSery *sery = [self.playHistoryDic objectAtIndex:selectionIndex.row];
[CoreDataManager deleteOneHistoryBySeryId:sery.seryId andVideoId:sery.latestVideo.videoId];
[indicesOfItemsToDelete addIndex:selectionIndex.row];
}
[self.playHistoryDic removeObjectsAtIndexes:indicesOfItemsToDelete];
[self.playHistoryTableView deleteRowsAtIndexPaths:selectedRows withRowAnimation:UITableViewRowAnimationAutomatic];
}
In your loop you're doing
[self.playHistoryDic removeObjectAtIndex:selectionIndex.row];
so you're changing the list of items. On each iteration you try to access an item, but after the first item the next one has moved, because you removed one. For each subsequent item this is worse.
Eventually you get to a point where you try to access an item but so many have been removed that you go past the end of the list, then you crash.
You should get an array of the items to be removed in your loop and remove them all at once after the loop has completed.
I'm writing a program that contains an array with multiple objects inside it. Then, I take specific objects from that array and store their indexes (from array1) in another array as NSNumbers. Now, I'm trying to reference the objects from array1 later on in the project by pulling the indexes out of array2 in a table view, but I keep getting an error that suggests that I'm getting the objects' memory addresses instead of the object itself. Code to follow:
self.indexArray = [NSMutableArray new];
for (Item *item in array1){
if (item.ID == comparedItem.ID){
NSUInteger num = [array1 indexOfObject:item];
NSNumber *numval = [NSNumber numberWithInteger:num];
[self.indexArray addObject:numval];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = (EditItemTableViewCell *)[tableView dequeueReusableCellWithIdentifier:#"quantityCell"];
int indexNum = [[self.indexArray objectAtIndex:(indexPath.row-1)] intValue];
NSString *descString = [NSString stringWithFormat:#"%# %# - %#",[[array1 objectAtIndex:indexNum]desc1],[[array1 objectAtIndex:indexNum]desc2],indexNum];
return cell;
}
So I'm getting an error on the line: int indexNum = [[self.indexArray objectAtIndex:(indexPath.row-1)] intValue]; that usually reads something like this: *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 4294967295 beyond bounds [0 .. 2]'. This is assuming I added 3 objects to the array before running the code above (hence the 0 .. 2). I'm assuming that 4294967295 is a memory address. Does anybody have any idea about how I can get the object itself instead?
Thank you!
4294967295 is -1 represented as an unsigned int32. In this context, it represents indexPath.row-1 == -1 or indexPath.row == 0.
I'm not sure why you're subtracting 1 from your indexPath.row, but you should add some safety to check the bounds to make sure your array lookup won't be fetching an element that's out of bounds.
if (indexPath.row == 0) // do something special
or
[self.indexArray objectAtIndex:indexPath.row]
I am entirely confused. I am modifying my table and it's crashing while up via adding/deleting rows and reloading sections. So I stopped adding and deleting rows, and it still crashes. So I stopped reloading sections and it still crashes. So I just put the code:
[self.tableView beginUpdates];
[self.tableView endUpdates];
with this code it crashes, without it's fine.
Why?
2014-09-12 21:52:41.257 weatherApp[12616:60b] * Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
* First throw call stack:
(0x2ea84f83 0x392ffccf 0x2e9bb7cb 0x3149eaef 0x3149cd8f 0x3149c843 0x31474e5b 0xeb82d 0xed059 0x313d48f7 0x3137bc27 0x3137b47d 0x312a1d59 0x30f1f62b 0x30f1ae3b 0x30f1accd 0x30f1a6df 0x30f1a4ef 0x30f6dc49 0x33d3a75d 0x2f770451 0x2ea44ea9 0x2ea4fa67 0x2ea4fa03 0x2ea4e1d7 0x2e9b8ebf 0x2e9b8ca3 0x33912663 0x3130514d 0x107599 0x3980cab7)
libc++abi.dylib: terminating with uncaught exception of type NSException
Edit: Be advised I am calling this update in cellForRowAtIndexPath because I need to check for an update as new cells are displayed. The code that causes this update is the [self updateSection code, which right now only runs begin updates, end updates.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
WFHNotamTableViewCell *cell = (WFHNotamTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellTableIdentifier forIndexPath:indexPath];
if (!cell)
{
NSLog(#"Dequeue blank cell");
}
NSLog(#"Loading %d - %d", indexPath.section, indexPath.row);
NSString *group;
NSString *icao = [metarManager getIdentifierAtTotalIndex:indexPath.section andReturnGroup:&group];
NSMutableDictionary *notamDict = [notamManager getNotamFor:icao atVisibleIndex:indexPath.row];
NSDate *lastUpdate = [notamDict objectForKey:#"timeDownloaded"];
[self updateSection:indexPath.section andLastUpdate:lastUpdate];
return cell;
Thank you!
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