I have an iOS app that has been working quite well in two languages – I know added some stuff, already keeping in mind using NSLocalizedString for localization etc. The english version of the app works fine so far, but the second language version crashes right after starting. Why?
One should note that I have not added all NSLocalizedStrings in my localizable.strings file. Furthermore, I have two separate Storyboards for both english an german. Any help would be greatly appreciated!
2014-12-18 18:14:36.083 Coverdale[48297:60b] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 2 beyond bounds [0 .. 1]'
*** First throw call stack:
(
0 CoreFoundation 0x002461e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x020538e5 objc_exception_throw + 44
2 CoreFoundation 0x001fa8b2 -[__NSArrayI objectAtIndex:] + 210
3 UIKit 0x0126a35f -[UITableViewDataSource tableView:indentationLevelForRowAtIndexPath:] + 127
4 UIKit 0x00fe3f34 -[UITableViewController tableView:indentationLevelForRowAtIndexPath:] + 61
5 UIKit 0x00e012cf __53-[UITableView _configureCellForDisplay:forIndexPath:]_block_invoke + 1786
6 UIKit 0x00d7581f +[UIView(Animation) performWithoutAnimation:] + 82
7 UIKit 0x00d75868 +[UIView(Animation) _performWithoutAnimation:] + 40
8 UIKit 0x00e00bd0 -[UITableView _configureCellForDisplay:forIndexPath:] + 108
9 UIKit 0x00e0813d -[UITableView _createPreparedCellForGlobalRow:withIndexPath:] + 442
10 UIKit 0x00e081f3 -[UITableView _createPreparedCellForGlobalRow:] + 69
11 UIKit 0x00de9ece -[UITableView _updateVisibleCellsNow:] + 2428
12 UIKit 0x00dfe6a5 -[UITableView layoutSubviews] + 213
13 UIKit 0x00d7e964 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 355
14 libobjc.A.dylib 0x0206582b -[NSObject performSelector:withObject:] + 70
15 QuartzCore 0x007c845a -[CALayer layoutSublayers] + 148
16 QuartzCore 0x007bc244 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
17 QuartzCore 0x007bc0b0 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 26
18 QuartzCore 0x007227fa _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 294
19 QuartzCore 0x00723b85 _ZN2CA11Transaction6commitEv + 393
20 QuartzCore 0x00724258 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 92
21 CoreFoundation 0x0020e36e __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 30
22 CoreFoundation 0x0020e2bf __CFRunLoopDoObservers + 399
23 CoreFoundation 0x001ec254 __CFRunLoopRun + 1076
24 CoreFoundation 0x001eb9d3 CFRunLoopRunSpecific + 467
25 CoreFoundation 0x001eb7eb CFRunLoopRunInMode + 123
26 GraphicsServices 0x0406b5ee GSEventRunModal + 192
27 GraphicsServices 0x0406b42b GSEventRun + 104
28 UIKit 0x00d0ff9b UIApplicationMain + 1225
29 Coverdale 0x0009b95d main + 141
30 libdyld.dylib 0x028b9701 start + 1
31 ??? 0x00000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
EDIT
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = #"Error";
if (indexPath.section == 0) {
if(indexPath.row == 0)
{ /*..*/}
}
return cell;
}
Turns out I needed to implement indentationLevelForRowAtIndexPath and just return 0.
Related
I've been trying to solve this problem for the past two days but I just can't find why it occurs, so I took it here to ask if you guys have any idea about it.
So I have a UITableView which I display some items in. I also have a UITableViewCell which I use as the cells.
I register the nib with my table view in the viewDidLoad:
[self.QuickPickDetailsTV registerNib:[UINib nibWithNibName:#"QuickPickItemCell"
bundle:[NSBundle mainBundle]]
forCellReuseIdentifier:#"QuickPickItemCellReuseID"];
Then in the tableView:cellForRowAtIndexPath:, I reuse, populate, and return the cell as below:
static NSString *CellIdentifier = #"QuickPickItemCellReuseID";
QuickPickItemCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[QuickPickItemCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
NSDictionary *quickPickItem = [self.myQuickPickItems objectAtIndex:indexPath.row];
NSString *theQuickPickItemName = [quickPickItem valueForKey:#"name"];
[cell.mName setFont:[UIFont fontWithName:#"OpenSans" size:16]];
[cell.mName setText:NSLocalizedString(theQuickPickItemName, nil)];
return cell;
The method above gets called for the number of items it should display (as expected), and crashes with an strange error after the last cell is returned:
2014-08-02 13:03:38.008 TestApp[49701:607] -[__NSArrayI length]: unrecognized selector sent to instance 0x2e92fde0
2014-08-02 13:03:38.010 TestApp[49701:607] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI length]: unrecognized selector sent to instance 0x2e92fde0'
*** First throw call stack:
(
0 CoreFoundation 0x044f21e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x033888e5 objc_exception_throw + 44
2 CoreFoundation 0x0458f243 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275
3 CoreFoundation 0x044e250b ___forwarding___ + 1019
4 CoreFoundation 0x044e20ee _CF_forwarding_prep_0 + 14
5 UIKit 0x01ff8463 -[UILabel _shadow] + 45
6 UIKit 0x01ff98c2 -[UILabel drawTextInRect:] + 70
7 UIKit 0x01ffbdfc -[UILabel drawRect:] + 98
8 UIKit 0x01eaa453 -[UIView(CALayerDelegate) drawLayer:inContext:] + 504
9 QuartzCore 0x00dbaf39 -[CALayer drawInContext:] + 123
10 QuartzCore 0x00dbae6a _ZL16backing_callbackP9CGContextPv + 96
11 QuartzCore 0x00ca94fc CABackingStoreUpdate_ + 2656
12 QuartzCore 0x00dbae02 ___ZN2CA5Layer8display_Ev_block_invoke + 93
13 QuartzCore 0x00def2d7 x_blame_allocations + 15
14 QuartzCore 0x00dbac6d _ZN2CA5Layer8display_Ev + 1519
15 QuartzCore 0x00dbaeb9 -[CALayer _display] + 33
16 QuartzCore 0x00dba676 _ZN2CA5Layer7displayEv + 144
17 QuartzCore 0x00dbae93 -[CALayer display] + 33
18 QuartzCore 0x00daf043 _ZN2CA5Layer17display_if_neededEPNS_11TransactionE + 323
19 QuartzCore 0x00daf0bc _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 38
20 QuartzCore 0x00d157fa _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 294
21 QuartzCore 0x00d16b85 _ZN2CA11Transaction6commitEv + 393
22 QuartzCore 0x00d17258 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 92
23 CoreFoundation 0x044ba36e __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 30
24 CoreFoundation 0x044ba2bf __CFRunLoopDoObservers + 399
25 CoreFoundation 0x04498254 __CFRunLoopRun + 1076
26 CoreFoundation 0x044979d3 CFRunLoopRunSpecific + 467
27 CoreFoundation 0x044977eb CFRunLoopRunInMode + 123
28 GraphicsServices 0x047e95ee GSEventRunModal + 192
29 GraphicsServices 0x047e942b GSEventRun + 104
30 UIKit 0x01e3bf9b UIApplicationMain + 1225
31 TestApp 0x00002c92 main + 130
32 TestApp 0x00002c05 start + 53
33 ??? 0x00000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
I have searched all my code but I don't send length to any object in my code. I don't know where is the crash coming from since it is shown to be on my main.m.
I have tried turning on NSZombies as well, but I didn't get any warnings about released objects.
Any help will be appreciated!
Thanks
Here is the line of code
cell.imageURL=[NSURL URLWithString:self.imageURLs[indexPath.row]];
Here is the error trace
[UITableViewCell setImageURL:]: unrecognized selector sent to instance 0x906da70
2014-07-24 17:16:50.049 MyApp_iOS[9443:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITableViewCell setImageURL:]: unrecognized selector sent to instance 0x906da70'
*** First throw call stack:
(
0 CoreFoundation 0x01a8c1e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x016ad8e5 objc_exception_throw + 44
2 CoreFoundation 0x01b29243 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275
3 CoreFoundation 0x01a7c50b ___forwarding___ + 1019
4 CoreFoundation 0x01a7c0ee _CF_forwarding_prep_0 + 14
5 MyApp_iOS 0x0007f518 -[BCDDogViewController tableView:cellForRowAtIndexPath:] + 504
6 UIKit 0x0046611f -[UITableView _createPreparedCellForGlobalRow:withIndexPath:] + 412
7 UIKit 0x004661f3 -[UITableView _createPreparedCellForGlobalRow:] + 69
8 UIKit 0x00447ece -[UITableView _updateVisibleCellsNow:] + 2428
9 UIKit 0x0045c6a5 -[UITableView layoutSubviews] + 213
10 UIKit 0x003dc964 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 355
11 libobjc.A.dylib 0x016bf82b -[NSObject performSelector:withObject:] + 70
12 QuartzCore 0x03ef745a -[CALayer layoutSublayers] + 148
13 QuartzCore 0x03eeb244 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
14 QuartzCore 0x03eeb0b0 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 26
15 QuartzCore 0x03e517fa _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 294
16 QuartzCore 0x03e52b85 _ZN2CA11Transaction6commitEv + 393
17 QuartzCore 0x03e53258 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 92
18 CoreFoundation 0x01a5436e __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 30
19 CoreFoundation 0x01a542bf __CFRunLoopDoObservers + 399
20 CoreFoundation 0x01a32254 __CFRunLoopRun + 1076
21 CoreFoundation 0x01a319d3 CFRunLoopRunSpecific + 467
22 CoreFoundation 0x01a317eb CFRunLoopRunInMode + 123
23 GraphicsServices 0x03a805ee GSEventRunModal + 192
24 GraphicsServices 0x03a8042b GSEventRun + 104
25 UIKit 0x0036df9b UIApplicationMain + 1225
26 MyApp_iOS 0x000577c2 main + 130
27 libdyld.dylib 0x0216d701 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
And the property in question is
#property(strong,nonatomic) NSURL *imageURL;
with setter
-(void)setImageURL:(NSURL *)imageURL
{
_imageURL=imageURL;
[self startDownloadingImage];
}
The strange thing is this used to work and about an hour or so ago it stops working and starts throwing this exception.
update
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
BCDDogImageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[BCDDogImageTableViewCell cellPrototypeIdentifier] forIndexPath:indexPath];
NSLog(#"THE CELL IS: %# ", cell);
cell.imageURL=[NSURL URLWithString:self.imageURLs[indexPath.row]];
return cell;
}
The error is because your cell is actually a UITableViewCell instance and not an instance of your custom cell class.
Be sure to register your custom cell class for the cell.
I got an crash bug on when tap on More button on tabbar. It just happened on iOS 7.1.
I created a class (MoreTableViewDelegate) which override UITableViewDelegate of More Table View
MoreNavigationController
UIViewController *moreViewController = tabBarController.moreNavigationController.topViewController;
UITableView *moreTableView = (UITableView*)moreViewController.view;
MoreTableViewDelegate *objMoreDelegate=[[MoreTableViewDelegate alloc]initWithDelegate:moreTableView.delegate];
moreTableView.delegate=objMoreDelegate;
This is the detail of MoreTableViewDelegate:
-(MoreTableViewDelegate *) initWithDelegate:(id<UITableViewDelegate>) delegate
{
originalDelegate=delegate;
[super init];
return self;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(isPoweredByMobicart==YES)
{
return 50;
}
else
{
return 60;
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
return [originalDelegate tableView:tableView didSelectRowAtIndexPath:indexPath];
}
But when I tap on the More button at tab bar, it's crashed and this is the log:
-[MoreTableViewDelegate _layoutCells]: unrecognized selector sent to instance 0xce7f300
2014-03-25 11:03:12.360 ForeverInt[961:90b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MoreTableViewDelegate _layoutCells]: unrecognized selector sent to instance 0xce7f300'
*** First throw call stack:
(
0 CoreFoundation 0x0336b1e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x02ee18e5 objc_exception_throw + 44
2 CoreFoundation 0x03408243 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275
3 CoreFoundation 0x0335b50b ___forwarding___ + 1019
4 CoreFoundation 0x0335b0ee _CF_forwarding_prep_0 + 14
5 UIKit 0x00bcc785 -[_UIMoreListTableView didMoveToWindow] + 108
6 UIKit 0x00adb478 -[UIView(Internal) _didMoveFromWindow:toWindow:] + 1642
7 UIKit 0x00aed68b -[UIScrollView _didMoveFromWindow:toWindow:] + 65
8 UIKit 0x00adb109 -[UIView(Internal) _didMoveFromWindow:toWindow:] + 763
9 UIKit 0x00ad296f __45-[UIView(Hierarchy) _postMovedFromSuperview:]_block_invoke + 158
10 UIKit 0x00ad27fb -[UIView(Hierarchy) _postMovedFromSuperview:] + 260
11 UIKit 0x00adddd4 -[UIView(Internal) _addSubview:positioned:relativeTo:] + 1875
12 UIKit 0x00ad0dba -[UIView(Hierarchy) addSubview:] + 56
13 UIKit 0x00db6c5b -[UINavigationTransitionView transition:fromView:toView:] + 501
14 UIKit 0x00db6a5e -[UINavigationTransitionView transition:toView:] + 55
15 UIKit 0x00bb7577 -[UINavigationController _startTransition:fromViewController:toViewController:] + 3186
16 UIKit 0x00bb78cc -[UINavigationController _startDeferredTransitionIfNeeded:] + 645
17 UIKit 0x00bb84e9 -[UINavigationController __viewWillLayoutSubviews] + 57
18 UIKit 0x00cf90d1 -[UILayoutContainerView layoutSubviews] + 213
19 UIKit 0x00ae0964 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 355
20 libobjc.A.dylib 0x02ef382b -[NSObject performSelector:withObject:] + 70
21 QuartzCore 0x01d9d45a -[CALayer layoutSublayers] + 148
22 QuartzCore 0x01d91244 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
23 QuartzCore 0x01d910b0 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 26
24 QuartzCore 0x01cf77fa _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 294
25 QuartzCore 0x01cf8b85 _ZN2CA11Transaction6commitEv + 393
26 QuartzCore 0x01db65b0 +[CATransaction flush] + 52
27 UIKit 0x00a6f9bb _UIApplicationHandleEventQueue + 13095
28 CoreFoundation 0x032f477f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
29 CoreFoundation 0x032f410b __CFRunLoopDoSources0 + 235
30 CoreFoundation 0x033111ae __CFRunLoopRun + 910
31 CoreFoundation 0x033109d3 CFRunLoopRunSpecific + 467
32 CoreFoundation 0x033107eb CFRunLoopRunInMode + 123
33 GraphicsServices 0x035c85ee GSEventRunModal + 192
34 GraphicsServices 0x035c842b GSEventRun + 104
35 UIKit 0x00a71f9b UIApplicationMain + 1225
36 ForeverInt 0x00086b9d main + 125
37 ForeverInt 0x000030d5 start + 53
)
libc++abi.dylib: terminating with uncaught exception of type NSException
How to solve this issue ? Please help me.
This question already has answers here:
Assertion failure in dequeueReusableCellWithIdentifier:forIndexPath:
(22 answers)
Closed 8 years ago.
here's the method causing the problem:
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"TableCell";
TableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier
forIndexPath:indexPath];
// Configure the cell...
int row = [indexPath row];
Books *book = [myBooks objectAtIndex:row];
cell.bookName.text = book.bookName;
return cell;
}
full log:
2014-02-05 23:19:09.458 Books[1425:a0b] *** Assertion failure in -[UITableView dequeueReusableCellWithIdentifier:forIndexPath:], /SourceCache/UIKit_Sim/UIKit-2903.2/UITableView.m:5251
2014-02-05 23:19:09.471 Books[1425:a0b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier TableCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
*** First throw call stack:
(
0 CoreFoundation 0x017395e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x014bc8b6 objc_exception_throw + 44
2 CoreFoundation 0x01739448 +[NSException raise:format:arguments:] + 136
3 Foundation 0x0109d23e -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 116
4 UIKit 0x003135e3 -[UITableView dequeueReusableCellWithIdentifier:forIndexPath:] + 170
5 Books 0x000032df -[TableViewController tableView:cellForRowAtIndexPath:] + 127
6 UIKit 0x0031dd2f -[UITableView _createPreparedCellForGlobalRow:withIndexPath:] + 412
7 UIKit 0x0031de03 -[UITableView _createPreparedCellForGlobalRow:] + 69
8 UIKit 0x00302124 -[UITableView _updateVisibleCellsNow:] + 2378
9 UIKit 0x003155a5 -[UITableView layoutSubviews] + 213
10 UIKit 0x00299dd7 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 355
11 libobjc.A.dylib 0x014ce81f -[NSObject performSelector:withObject:] + 70
12 QuartzCore 0x03af372a -[CALayer layoutSublayers] + 148
13 QuartzCore 0x03ae7514 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
14 QuartzCore 0x03af3675 -[CALayer layoutIfNeeded] + 160
15 UIKit 0x00354ca3 -[UIViewController window:setupWithInterfaceOrientation:] + 304
16 UIKit 0x00273d27 -[UIWindow _setRotatableClient:toOrientation:updateStatusBar:duration:force:isRotating:] + 5212
17 UIKit 0x002728c6 -[UIWindow _setRotatableClient:toOrientation:updateStatusBar:duration:force:] + 82
18 UIKit 0x00272798 -[UIWindow _setRotatableViewOrientation:updateStatusBar:duration:force:] + 117
19 UIKit 0x00272820 -[UIWindow _setRotatableViewOrientation:duration:force:] + 67
20 UIKit 0x002718ba __57-[UIWindow _updateToInterfaceOrientation:duration:force:]_block_invoke + 120
21 UIKit 0x0027181c -[UIWindow _updateToInterfaceOrientation:duration:force:] + 400
22 UIKit 0x00272573 -[UIWindow setAutorotates:forceUpdateInterfaceOrientation:] + 870
23 UIKit 0x00275b66 -[UIWindow setDelegate:] + 449
24 UIKit 0x00346dc7 -[UIViewController _tryBecomeRootViewControllerInWindow:] + 180
25 UIKit 0x0026b7cc -[UIWindow addRootViewControllerViewIfPossible] + 609
26 UIKit 0x0026b947 -[UIWindow _setHidden:forced:] + 312
27 UIKit 0x0026bbdd -[UIWindow _orderFrontWithoutMakingKey] + 49
28 UIKit 0x0027644a -[UIWindow makeKeyAndVisible] + 65
29 UIKit 0x002298e0 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 1851
30 UIKit 0x0022dfb8 -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 824
31 UIKit 0x0024242c -[UIApplication handleEvent:withNewEvent:] + 3447
32 UIKit 0x00242999 -[UIApplication sendEvent:] + 85
33 UIKit 0x0022fc35 _UIApplicationHandleEvent + 736
34 GraphicsServices 0x0368c2eb _PurpleEventCallback + 776
35 GraphicsServices 0x0368bdf6 PurpleEventCallback + 46
36 CoreFoundation 0x016b4dd5 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 53
37 CoreFoundation 0x016b4b0b __CFRunLoopDoSource1 + 523
38 CoreFoundation 0x016df7ec __CFRunLoopRun + 2156
39 CoreFoundation 0x016deb33 CFRunLoopRunSpecific + 467
40 CoreFoundation 0x016de94b CFRunLoopRunInMode + 123
41 UIKit 0x0022d6ed -[UIApplication _run] + 840
42 UIKit 0x0022f94b UIApplicationMain + 1225
43 Books 0x000038bd main + 141
44 libdyld.dylib 0x01d75725 start + 0
45 ??? 0x00000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
You can directly set the identifier using the storyboard like this.
From the documentation:
Important: You must register a class or nib file using the
registerNib:forCellReuseIdentifier: or
registerClass:forCellReuseIdentifier: method before calling this
method.
You need to register the cell as a usable class for that identifier. Call this after you initialize your UITableView (probably in viewDidLoad)
[self.tableView registerClass:[TableCell class] forCellReuseIdentifier:CellIdentifier]
This assumes CellIdentifier has been moved somewhere where it can be accessed from here.
Try this:
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"TableCell";
TableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(!cell)
{
cell = [[[TableCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell...
int row = [indexPath row];
Books *book = [myBooks objectAtIndex:row];
cell.bookName.text = book.bookName;
return cell;
}
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] ()