I am trying to get the contents of my UITableViewCell before I delete it from my Core Data model in order to act on its contents. If there is only one item in the Core Data model and I go to delete it from my UITableView, the app will only sometimes throw an error of 2014-01-05 11:10:26.189 Nibbles[43609:70b] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[_PFArray objectAtIndex:]: index (2008) beyond bounds (1)'
My biggest confusion is that this only happens with one item in the UITableView and doesn't happen 100% of the time. If you need to see any other code, please let me know.
Here is the code I am using. The bolded line is the one causing the error since it never makes it past there to NSLog to the console.
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
NSLog(#"Delete button pressed!");
FoodListCell *cell = (FoodListCell*)[tableView cellForRowAtIndexPath:indexPath];
NSLog(#"DEBUG | Selected Cell: %#", cell);
NSString *foodOrActivity = cell.foodNameLabel.text;
NSString *points = cell.foodPointsLabel.text;
NSManagedObjectContext *localContext = [NSManagedObjectContext MR_contextForCurrentThread];
[[self.fetchedResultsController objectAtIndexPath:indexPath] MR_deleteInContext:localContext];
[localContext MR_saveOnlySelfAndWait];
NSIndexPath *path = [NSIndexPath indexPathForRow:indexPath.row inSection:0];
NSManagedObject *deleteObject = [_fetchedResultsController objectAtIndexPath:path];
[managedObjectContext deleteObject:deleteObject];
}
And here is the full stack trace:
2014-01-05 11:10:26.189 Nibbles[43609:70b] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[_PFArray objectAtIndex:]: index (2008) beyond bounds (1)'
*** First throw call stack:
(
0 CoreFoundation 0x01cd45e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x01a578b6 objc_exception_throw + 44
2 CoreFoundation 0x01cd43bb +[NSException raise:format:] + 139
3 CoreData 0x00280755 -[_PFArray objectAtIndex:] + 133
4 CoreData 0x002f9778 -[_PFMutableProxyArray objectAtIndex:] + 120
5 CoreData 0x00382c1f -[NSFetchedResultsController objectAtIndexPath:] + 255
6 Nibbles 0x0000660d -[FoodListViewController tableView:commitEditingStyle:forRowAtIndexPath:] + 685
7 UIKit 0x008b5ba3 -[UITableView animateDeletionOfRowWithCell:] + 107
8 UIKit 0x00a35695 -[UITableViewCell _swipeDeleteButtonPushed] + 70
9 libobjc.A.dylib 0x01a69874 -[NSObject performSelector:withObject:withObject:] + 77
10 UIKit 0x007c70c2 -[UIApplication sendAction:to:from:forEvent:] + 108
11 UIKit 0x007c704e -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 61
12 UIKit 0x008bf0c1 -[UIControl sendAction:to:forEvent:] + 66
13 UIKit 0x008bf484 -[UIControl _sendActionsForEvents:withEvent:] + 577
14 UIKit 0x008be733 -[UIControl touchesEnded:withEvent:] + 641
15 UIKit 0x00b39c7f _UIGestureRecognizerUpdate + 7166
16 UIKit 0x0080419a -[UIWindow _sendGesturesForEvent:] + 1291
17 UIKit 0x008050ba -[UIWindow sendEvent:] + 1030
18 UIKit 0x007d8e86 -[UIApplication sendEvent:] + 242
19 UIKit 0x007c318f _UIApplicationHandleEventQueue + 11421
20 CoreFoundation 0x01c5d83f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
21 CoreFoundation 0x01c5d1cb __CFRunLoopDoSources0 + 235
22 CoreFoundation 0x01c7a29e __CFRunLoopRun + 910
23 CoreFoundation 0x01c79ac3 CFRunLoopRunSpecific + 467
24 CoreFoundation 0x01c798db CFRunLoopRunInMode + 123
25 GraphicsServices 0x0285e9e2 GSEventRunModal + 192
26 GraphicsServices 0x0285e809 GSEventRun + 104
27 UIKit 0x007c5d3b UIApplicationMain + 1225
28 Nibbles 0x000236ad main + 141
29 libdyld.dylib 0x031b470d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
The conventional pattern is to access the model (not through a table cell, but directly from the model). Do whatever you need to do with it, then remove it from your model, then remove it from your table...
The error implies that the localContext being accessed this method is different from (or is in a different state than) the MOC that reports the number of items in the model (used in numberOfRowsInSection:)
Before moving on to recording that deleted object's state, or deleting it. Fix the code that gets that object and just NSLog it. The code that gets that object should exactly match the code that gets it in your cellForRowAtIndexPath:, and the MOC used there should exactly match the numberOfRowsInSection: MOC.
NSIndexPath *path = [NSIndexPath indexPathForRow:indexPath.row inSection:0];
NSManagedObject *deleteObject = [_fetchedResultsController objectAtIndexPath:path];
[managedObjectContext deleteObject:deleteObject];
Here you are getting the index path for the row in section 0 and deleting it.
You already have the index path for the row/cell/section that you want to delete, so why are you trying to delete that row only in section 0?
Try a breakpoint at this line and add some logging to show what row in section 0 is trying to be deleted.
Related
I have Maintableview(a UIViewController) with contents and detailview(a UIViewController) that show detail for each of cells in Maintableview.
I have added a "favorite" button in "detailview" that user can add to Favoritetableview(a UIViewController) and every thing work fine.
Now I have added "add to favorite" in Maintableview with swipe to left.
It adds contents successfully to Favoritetableview but when I touch that cell in Favoritetableview the app crashes.
Below is the console logs for the crash:
Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
*** First throw call stack:
(
0 CoreFoundation 0x000000010a871d85 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010a2e3deb objc_exception_throw + 48
2 CoreFoundation 0x000000010a75a934 -[__NSArrayI objectAtIndex:] + 164
3 iranbirds 0x00000001075cf672 -[FavoriteTableViewController prepareForSegue:sender:] + 530
4 UIKit 0x00000001090c55d5 -[UIStoryboardSegueTemplate _performWithDestinationViewController:sender:] + 369
5 UIKit 0x00000001090c5433 -[UIStoryboardSegueTemplate _perform:] + 82
6 UIKit 0x0000000108b1b5f8 -[UIViewController performSegueWithIdentifier:sender:] + 99
7 iranbirds 0x00000001075cf42d -[FavoriteTableViewController tableView:didSelectRowAtIndexPath:] + 189
8 UIKit 0x0000000108ac51c6 -[UITableView _selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:] + 1887
9 UIKit 0x0000000108ac541b -[UITableView _userSelectRowAtPendingSelectionIndexPath:] + 388
10 UIKit 0x0000000108989f62 _runAfterCACommitDeferredBlocks + 317
11 UIKit 0x000000010899de4c _cleanUpAfterCAFlushAndRunDeferredBlocks + 95
12 UIKit 0x00000001089aa147 _afterCACommitHandler + 90
13 CoreFoundation 0x000000010a796c37 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
14 CoreFoundation 0x000000010a796ba7 __CFRunLoopDoObservers + 391
15 CoreFoundation 0x000000010a78c7fb __CFRunLoopRun + 1147
16 CoreFoundation 0x000000010a78c0f8 CFRunLoopRunSpecific + 488
17 GraphicsServices 0x000000010be6bad2 GSEventRunModal + 161
18 UIKit 0x000000010897df09 UIApplicationMain + 171
19 iranbirds 0x00000001075d32ef main + 111
20 libdyld.dylib 0x000000010ba6492d start + 1
21 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
favorite table view:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSIndexPath *indexPath = (NSIndexPath *)sender;
Favorite *fav = (Favorite *)[self.fetchedResultsController objectAtIndexPath:indexPath];
NSString *combinedName = fav.name;
if ([segue.identifier isEqualToString:#"FavoriteBirdDetail"])
{
GeneralViewController *detailViewController = (GeneralViewController*)[segue destinationViewController];
detailViewController.birdName = [combinedName componentsSeparatedByString:#"^"][0];;
detailViewController.sciName = [combinedName componentsSeparatedByString:#"^"][1];;
detailViewController.managedOjbectContext = self.managedOjbectContext;
}
}
This line cause error:
detailViewController.sciName = [combinedName componentsSeparatedByString:#"^"][1];
Any help would be appreciated.
detailViewController.sciName = [combinedName componentsSeparatedByString:#"^"][1];
Well, look at this code - you are getting second object from an array by subscript syntax. And the code would work if an array contains at least 2 items.
It looks like your array contains zero or one element, that is why app crashes when you try to get an element by 1 index - [1], which means the second element.
To prevent crash you should check an array count property before:
NSArray *anArray = [combinedName componentsSeparatedByString:#"^"];
if anArray.count >= 2 {
detailViewController.sciName = anArray[1];
}
This my code ,i am removing multiple values on my condition
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
PhotoCan *cell=[collectionView cellForItemAtIndexPath:indexPath];
UIImage *getimg=[dicPhotoStore objectForKey:#(indexPath.row)];
BOOL integer=[dic objectForKey:#(indexPath.row)];
if(integer)
{
for(id img in arrFinalStore)
{
if(img==getimg)
{
NSLock *arrayLock = [[NSLock alloc] init];
[arrayLock lock];
[arrFinalStore removeObject:img];
NSLog(#"crash inside");
[arrayLock unlock];
}
}
#synchronized(self)
{
[dic removeObjectForKey:#(indexPath.row)];
[dicPhotoStore removeObjectForKey:#(indexPath.row)];
}
NSLog(#"crashoutside");
NSLog(#"inside false");
}
else
{
[dicPhotoStore setObject:cell.imgView.image forKey:#(indexPath.row)];
[arrFinalStore addObject:cell.imgView.image];
[dic setObject:#"1" forKey:#(indexPath.row)];
}
[_collection reloadData];
}
I am understood something goes wrong trying to find out this error ,please help me to overcome this .
This is error :
*** Terminating app due to uncaught exception 'NSGenericException', reason: '*** Collection <__NSArrayM: 0x7f881a6b1900> was mutated while being enumerated.'
*** First throw call stack:
(
0 CoreFoundation 0x000000010299fe65 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x0000000102418deb objc_exception_throw + 48
2 CoreFoundation 0x000000010299f7c4 __NSFastEnumerationMutationHandler + 132
3 PhotoCollageCanvas 0x000000010056878f -[photoAssets collectionView:didSelectItemAtIndexPath:] + 767
4 UIKit 0x0000000103afb4a7 -[UICollectionView _selectItemAtIndexPath:animated:scrollPosition:notifyDelegate:] + 701
5 UIKit 0x0000000103b1d049 -[UICollectionView touchesEnded:withEvent:] + 574
6 UIKit 0x00000001034b6ef7 forwardTouchMethod + 349
7 UIKit 0x00000001034b6fc0 -[UIResponder touchesEnded:withEvent:] + 49
8 UIKit 0x00000001034b6ef7 forwardTouchMethod + 349
9 UIKit 0x00000001034b6fc0 -[UIResponder touchesEnded:withEvent:] + 49
10 UIKit 0x0000000103783ede _UIGestureRecognizerUpdate + 10279
11 UIKit 0x0000000103319f8a -[UIWindow _sendGesturesForEvent:] + 1137
12 UIKit 0x000000010331b1c0 -[UIWindow sendEvent:] + 849
13 UIKit 0x00000001032c9b66 -[UIApplication sendEvent:] + 263
14 UIKit 0x00000001032a3d97 _UIApplicationHandleEventQueue + 6844
15 CoreFoundation 0x00000001028cba31 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
16 CoreFoundation 0x00000001028c195c __CFRunLoopDoSources0 + 556
17 CoreFoundation 0x00000001028c0e13 __CFRunLoopRun + 867
18 CoreFoundation 0x00000001028c0828 CFRunLoopRunSpecific + 488
19 GraphicsServices 0x00000001058d8ad2 GSEventRunModal + 161
20 UIKit 0x00000001032a9610 UIApplicationMain + 171
21 Photo 0x0000000100566a5f main + 111
22 libdyld.dylib 0x000000010697b92d start + 1
23 ??? 0x0000000000000001 0x0 + 1
)
What do you think that lock would do? It does exactly nothing. You created a lock, locked it, unlocked it. Since that code is the only code with access to the lock, that lock cannot possibly prevent anyone from doing anything.
And it wouldn't solve your problem anyway. Someone is enumerating that array. Any attempt to modify it, as you are trying, while it is enumerated, will crash.
Your code is (with lines removed)
for(id img in arrFinalStore)
{
[arrFinalStore removeObject:img];
}
That code is absolutely going to crash, and there is nothing that can stop it from crashing. You cannot remove an object from an array while iteration.
You cannot remove element while iterating through array
[arrFinalStore removeObject:img];
just use [arrFinalStore removeObject:img];
You cannot remove element while iterating through array, so you can create temp array with all unnecessary objects and then call - (void)removeObjectsInArray:(NSArray<ObjectType> *)otherArray
So your code is:
NSMutableArray *tempArray = [NSMutableArray array];
for(id img in arrFinalStore)
{
if(img==getimg)
{
[tempArray addObject:img];
}
}
[arrFinalStore removeObjectsInArray:tempArray];
also you can use simple for (int i = arrfinalstoer.count - 1; i > 0; --i) for enumeration.
Its not the issue with the lock. But the issue is while iterating you are trying to remove the element which means you doing concurrent modification which throws exception.
here is an example you can use
for (item in originalArrayOfItems) {
if ([item shouldBeDiscarded])
[discardedItems addObject:item];}
[originalArrayOfItems removeObjectsInArray:discardedItems];
my code is:
- (void) tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath{
if (editingStyle == UITableViewCellEditingStyleDelete){
/* First remove this object from the source */
[self.allRows removeObjectAtIndex:indexPath.row];
/* Then remove the associated cell from the Table View */
[tableView deleteRowsAtIndexPaths:#[indexPath]
withRowAnimation:UITableViewRowAnimationFade];
}
}
i am getting error as follows
2015-02-16 11:57:32.413 SimpleTable[1590:45462] -[ViewController refreshControl]: unrecognized selector sent to instance 0x7fa1d2d936c0
2015-02-16 11:57:32.415 SimpleTable[1590:45462] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ViewController refreshControl]: unrecognized selector sent to instance 0x7fa1d2d936c0'
*** First throw call stack:
(
0 CoreFoundation 0x000000010d738f35 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010d3d1bb7 objc_exception_throw + 45
2 CoreFoundation 0x000000010d74004d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x000000010d69827c ___forwarding___ + 988
4 CoreFoundation 0x000000010d697e18 _CF_forwarding_prep_0 + 120
5 UIKit 0x000000010db268be -[UIApplication sendAction:to:from:forEvent:] + 75
6 UIKit 0x000000010db268be -[UIApplication sendAction:to:from:forEvent:] + 75
7 UIKit 0x000000010dc2d410 -[UIControl _sendActionsForEvents:withEvent:] + 467
8 UIKit 0x000000010dc2c7df -[UIControl touchesEnded:withEvent:] + 522
9 UIKit 0x000000010db6c308 -[UIWindow _sendTouchesForEvent:] + 735
10 UIKit 0x000000010db6cc33 -[UIWindow sendEvent:] + 683
11 UIKit 0x000000010db399b1 -[UIApplication sendEvent:] + 246
12 UIKit 0x000000010db46a7d _UIApplicationHandleEventFromQueueEvent + 17370
13 UIKit 0x000000010db22103 _UIApplicationHandleEventQueue + 1961
14 CoreFoundation 0x000000010d66e551 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
15 CoreFoundation 0x000000010d66441d __CFRunLoopDoSources0 + 269
16 CoreFoundation 0x000000010d663a54 __CFRunLoopRun + 868
17 CoreFoundation 0x000000010d663486 CFRunLoopRunSpecific + 470
18 GraphicsServices 0x0000000110d079f0 GSEventRunModal + 161
19 UIKit 0x000000010db25420 UIApplicationMain + 1282
20 SimpleTable 0x000000010cea27d3 main + 115
21 libdyld.dylib 0x000000010fcc8145 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
I think your TableView may be getting released after you create it and the method ends. You probably need to create an instance variable inside in View controller that references it.
#property (strong, nonatomic) IBOutlet UITableView *tablview;
Then remove the associated cell from the Table View
[self.tablview deleteRowsAtIndexPaths:#[indexPath]
withRowAnimation:UITableViewRowAnimationFade];
May help you..
Try this
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
//remove the row from data model
[tableData removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
I got this error message.
GoogleAnalytics 2.0b3 void GAIUncaughtExceptionHandler(NSException *)
(GAIUncaughtExceptionHandler.m:41): Uncaught exception: *** -[__NSArrayM objectAtIndex:]:
index 2 beyond bounds [0 .. 1]
libc++abi.dylib: terminate_handler unexpectedly threw an exception
When I trying to zoom page, I got crash and this error in crashlytics.
Thread : Fatal Exception: NSRangeException
0 CoreFoundation 0x31a202a3 __exceptionPreprocess + 162
1 libobjc.A.dylib 0x3974897f objc_exception_throw + 30
2 CoreFoundation 0x3196bb75 -[__NSArrayM objectAtIndex:] + 164
3 TestApp 0x000b1bfb -[EpisodeTableViewDelegate
tableView:didSelectRowAtIndexPath:] (EpisodeTableViewDelegate.m:155)
4 UIKit 0x338ea28d -[UITableView
_selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:] + 876
5 UIKit 0x3396cf81 -[UITableView
_userSelectRowAtPendingSelectionIndexPath:] + 156
6 Foundation 0x3232e277 __NSFireDelayedPerform + 450
7 CoreFoundation 0x319f55df __
CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 14
8 CoreFoundation 0x319f5291 __CFRunLoopDoTimer + 272
9 CoreFoundation 0x319f3f01 __CFRunLoopRun + 1232
10 CoreFoundation 0x31966ebd CFRunLoopRunSpecific + 356
11 CoreFoundation 0x31966d49 CFRunLoopRunInMode + 104
12 GraphicsServices 0x3553f2eb GSEventRunModal + 74
13 UIKit 0x3387c301 UIApplicationMain + 1120
14 Tapastic 0x00075cb7 main (main.m:16)
15 Tapastic 0x000753b8 start
My code is as below:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:NO];
if (indexPath.section == 1) {
Episode *episode = [[Episode alloc] init];
[episode bindProperties:[self.pager.contents objectAtIndex:indexPath.row]];
[self.baseController performSegueWithIdentifier:SEGUE_EPISODE_VIEW sender:episode];
}
}
I cannot solve this problem. How can I fix it? If possible, please let know.
Thanks in advance.
You're calling objectAtIndex on the episode array with index 2, when there are only two objects in the array. Array objects are numbered starting with 0, so an array with two objects only has elements 0 and 1.
It sounds like you're not setting up your UITableView correctly. If there are only two objects in the array backing the table, there should only be two rows in the table.
My developing iOS application is sometimes terminated after the method addSubview: is called in the application delegate (but I'm not sure removeFromSuperview: also involved):
[self.tabBarController.view removeFromSuperview];
[self.window addSubview:navigationController.view];
What that showed in the log is:
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSMutableArray objectAtIndex:]: index 0 beyond bounds for empty array'
*** Call stack at first throw:
(
0 CoreFoundation 0x019b75a9 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x01c41313 objc_exception_throw + 44
2 CoreFoundation 0x019ad0a5 -[__NSArrayM objectAtIndex:] + 261
3 <app_name> 0x000186dd -[TableViewController tableView:numberOfRowsInSection:] + 205
4 UIKit 0x00a742b7 -[UISectionRowData refreshWithSection:tableView:tableViewRowData:] + 1834
5 UIKit 0x00a71eb1 -[UITableViewRowData numberOfRowsInSection:] + 110
6 UIKit 0x00b60807 -[UISearchDisplayController _updateNoSearchResultsMessageVisiblity] + 115
7 Foundation 0x01029669 _nsnote_callback + 145
8 CoreFoundation 0x0198f9f9 __CFXNotificationPost_old + 745
9 CoreFoundation 0x0190e93a _CFXNotificationPostNotification + 186
10 Foundation 0x0101f20e -[NSNotificationCenter postNotificationName:object:userInfo:] + 134
11 UIKit 0x008df649 -[UIWindow _setRotatableClient:toOrientation:updateStatusBar:duration:force:] + 4581
12 UIKit 0x008d9254 -[UIWindow _setRotatableViewOrientation:duration:force:] + 89
13 UIKit 0x008db38f -[UIWindow setAutorotates:forceUpdateInterfaceOrientation:] + 794
14 UIKit 0x008d9081 -[UIWindow setDelegate:] + 252
15 UIKit 0x00964dee -[UIViewController _tryBecomeRootViewControllerInWindow:] + 106
16 UIKit 0x00975340 -[UINavigationController viewDidMoveToWindow:shouldAppearOrDisappear:] + 72
17 UIKit 0x008f0e4b -[UIView(Internal) _didMoveFromWindow:toWindow:] + 918
18 UIKit 0x008efa60 -[UIView(Hierarchy) _postMovedFromSuperview:] + 166
19 UIKit 0x008e8750 -[UIView(Internal) _addSubview:positioned:relativeTo:] + 1080
20 UIKit 0x008e6aa3 -[UIView(Hierarchy) addSubview:] + 57
21 <app_name> 0x00004971 -[AppDelegate requestFinished:] + 993
22 <app_name> 0x0003e65b -[ASIHTTPRequest reportFinished] + 171
23 Foundation 0x0103e94e __NSThreadPerformPerform + 251
24 CoreFoundation 0x019988ff __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
25 CoreFoundation 0x018f688b __CFRunLoopDoSources0 + 571
26 CoreFoundation 0x018f5d86 __CFRunLoopRun + 470
27 CoreFoundation 0x018f5840 CFRunLoopRunSpecific + 208
28 CoreFoundation 0x018f5761 CFRunLoopRunInMode + 97
29 GraphicsServices 0x01db11c4 GSEventRunModal + 217
30 GraphicsServices 0x01db1289 GSEventRun + 115
31 UIKit 0x008c5c93 UIApplicationMain + 1160
32 <app_name> 0x00002a30 main + 192
33 <app_name> 0x00002255 start + 53
)
terminate called throwing an exception
How can I find out the reason why the app is terminated by this log and solve it? Thank you.
Note: Edit 1 and Edit 2 were removed.
Edit 3: This implemented code of tableView:numberOfRowsInSection: method works for me:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == self.searchDisplayController.searchResultsTableView)
{
if (filteredSectionListOfAddressBook && filteredSectionListOfAddressBook.count)
{
return [[filteredSectionListOfAddressBook objectAtIndex:section] count];
}
else
{
return 0;
}
}
else if (tableView == self.tableView)
{
if (sectionListOfAddressBook && sectionListOfAddressBook.count)
{
return [[sectionListOfAddressBook objectAtIndex:section] count];
}
else
{
return 0;
}
}
return 0;
}
In your stack trace - there is a tableview - you need to ensure that there is data to populate the table. Thats where this error is coming from
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSMutableArray objectAtIndex:]: index 0 beyond bounds for empty array
You could for the moment ensure there is no data by making sure that
tableView:numberOfRowsInSection: returns 0;
Have you implemented the TableViews delegate methods?
Read about it here About Table Views in iOS-Based Applications
here UITableViewDataSource Protocol Reference
and here UITableViewDelegate Protocol Reference
I just ran into a problem like this in my code, and I found that it was caused by the search delegate holding on to the search bar and the table ref. If a set these properties to nil in searchDisplayControllerDidEndSearch then it got rid of the problem. It was likely caused by a circular ref loop. The stack trace looked like so:
[__NSArrayM _updateNoSearchResultsMessageVisiblity]: unrecognized selector sent to instance 0x21c350
#0 0x3369a1c8 in objc_exception_throw ()
#1 0x338b5aca in -[NSObject doesNotRecognizeSelector:] ()
#2 0x338b4944 in __forwarding__ ()
#3 0x3380f680 in _forwarding_prep_0__ ()
#4 0x30e5aff8 in -[UISearchResultsTableView _numberOfRowsDidChange] ()
#5 0x30d6c3b2 in -[UITableView noteNumberOfRowsChanged] ()
#6 0x30d6bf4e in -[UITableView reloadData] ()