简体   繁体   中英

UitableView working fine on iOS8.4 (Xcode 6.4) but crashes on iOS9.1(Xcode 7.1.1)

The UITableView works fine when I am trying to use it through Xcode6.4, but when I'm trying it through Xcode 7.1.1 it crashes. I guess it's because of the iOS versions. I am attaching the crash log. Kindly suggest what the best way to fix it is. I am quite sure that: 1) Datasource and Delegates are working fine. 2) Numberofrows and heightOfRow methods are also getting called, but it crashes before the cellForRowAtIndexPath gets called.

Here is the log:

2016-02-05 15:34:27.400 Autograph24[3720:87023] * Assertion failure in -[UITableViewRowData _assertValidIndexPath:allowEmptySection:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3512.29.5/UITableViewRowData.m:2310 2016-02-05 15:34:27.506 Autograph24[3720:87023] * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid indexPath' *** First throw call stack: ( 0 CoreFoundation 0x00000001075c9f45 exceptionPreprocess + 165 1 libobjc.A.dylib
0x0000000107956deb objc_exception_throw + 48 2 CoreFoundation
0x00000001075c9daa +[NSException raise:format:arguments:] + 106 3
Foundation 0x0000000106e9b5ee -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 198 4
UIKit 0x0000000108d11412 -[UITableViewRowData _assertValidIndexPath:allowEmptySection:] + 963 5 UIKit 0x0000000108d11465 -[UITableViewRowData ensureHeightsFaultedInForIndexPath:availHeight:edgeInset:scrollPosition:] + 55 6 UIKit 0x0000000108af50c6 -[UITableView _contentOffsetForScrollingToRowAtIndexPath:atScrollPosition:] + 2510 7 UIKit 0x0000000108af59b9 -[UITableView scrollToRowAtIndexPath:atScrollPosition:animated:] + 39 8 Autograph24 0x0000000106b1b2ad -[DashboardVC scrollToToday] + 685 9 Autograph24 0x0000000106b20289 -[DashboardVC changeCalendarStyle] + 313 10 Autograph24 0x0000000106b20144 -[DashboardVC resizeScreenForPortrait] + 788 11 Autograph24
0x0000000106b1f6a0 -[DashboardVC rotateScreen] + 176 12 Autograph24 0x0000000106b15e76 -[DashboardVC viewDidLoad] + 166 13 UIKit
0x0000000108b49cc4 -[UIViewController loadViewIfRequired] + 1198 14 UIKit 0x0000000108b4fc7b -[UIViewController __viewWillAppear:] + 120 15 UIKit 0x0000000108b7fa37 -[UINavigationController _startCustomTransition:] + 1203 16 UIKit 0x0000000108b8fcdb -[UINavigationController _startDeferredTransitionIfNeeded:] + 712 17 UIKit 0x0000000108b90cea -[UINavigationController __viewWillLayoutSubviews] + 57 18 UIKit 0x0000000108d36c85 -[UILayoutContainerView layoutSubviews] + 248 19 UIKit 0x0000000108a6be40 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 710 20 QuartzCore 0x000000010736359a -[CALayer layoutSublayers] + 146 21 QuartzCore
0x0000000107357e70 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366 22 QuartzCore 0x0000000107357cee _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24 23 QuartzCore 0x000000010734c475 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 277 24 QuartzCore 0x0000000107379c0a _ZN2CA11Transaction6commitEv + 486 25 QuartzCore 0x000000010737a37c _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 92 26 CoreFoundation 0x00000001074f5947 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION + 23 27 CoreFoundation 0x00000001074f58b7 __CFRunLoopDoObservers + 391 28 CoreFoundation 0x00000001074eb50b __CFRunLoopRun + 1147 29 CoreFoundation
0x00000001074eae08 CFRunLoopRunSpecific + 488 30 GraphicsServices
0x000000010b1b5ad2 GSEventRunModal + 161 31 UIKit
0x00000001089b730d UIApplicationMain + 171 32 Autograph24
0x0000000106b15d9f main + 111 33 libdyld.dylib
0x000000010aac192d start + 1 34 ???
0x0000000000000001 0x0 + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException

You have an invalid indexpath. That means that either the section or the row or both is not right. If you have a table with 2 sections and 2 rows per section then indexpath 0-3 is invalid, because section 0 does not provide a row 3. Similarly, indexpath 2-1 would be invalid, because section 2 does not exist (only 0 and 1).

As for how to fix this, first double check if all required methods are called and your data is correctly initialised. Then sprinkle breakpoints around your class. Mainly where you use any kind of indexpath, and see where it tries to read from outside your data.

Learn to read crash logs. In most cases and certainly in this case, they contain all the relevant information:

2016-02-05 15:34:27.400 Autograph24[3720:87023] * Assertion failure in -[UITableViewRowData _assertValidIndexPath:allowEmptySection:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3512.29.5/UITableViewRowData.m:2310
2016-02-05 15:34:27.506 Autograph24[3720:87023] * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid indexPath'

It clearly says: reason: 'Invalid indexPath'

So first hint: an invalid index path (which is a well known thing if you work with UITableViews) was involved.

Next follows the stack trace where the error ocurred. The first few stack locations are within Apple's code. Usually skip them. But occasionally, they contain interesting hints. Here a function assertValidIndexPath:allowEmptySection: . That again shows that the index path is the problem.

*** First throw call stack: (
0 CoreFoundation 0x00000001075c9f45 exceptionPreprocess + 165
1 libobjc.A.dylib 0x0000000107956deb objc_exception_throw + 48
2 CoreFoundation 0x00000001075c9daa +[NSException raise:format:arguments:] + 106
3 Foundation 0x0000000106e9b5ee -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 198
4 UIKit 0x0000000108d11412 -[UITableViewRowData _assertValidIndexPath:allowEmptySection:] + 963
5 UIKit 0x0000000108d11465 -[UITableViewRowData ensureHeightsFaultedInForIndexPath:availHeight:edgeInset:scrollPosition:] + 55
6 UIKit 0x0000000108af50c6 -[UITableView _contentOffsetForScrollingToRowAtIndexPath:atScrollPosition:] + 2510
7 UIKit 0x0000000108af59b9 -[UITableView scrollToRowAtIndexPath:atScrollPosition:animated:] + 39

Here your code follows. Look at the line above and below. It says that your function [DashboardVC scrollToToday] has called the function [UITableView scrollToRowAtIndexPath:atScrollPosition:animated:] . This is where the error was triggered in your code.

8 Autograph24 0x0000000106b1b2ad -[DashboardVC scrollToToday] + 685
9 Autograph24 0x0000000106b20289 -[DashboardVC changeCalendarStyle] + 313
10 Autograph24 0x0000000106b20144 -[DashboardVC resizeScreenForPortrait] + 788
11 Autograph24 0x0000000106b1f6a0 -[DashboardVC rotateScreen] + 176
12 Autograph24 0x0000000106b15e76 -[DashboardVC viewDidLoad] + 166

This is the next interesting location. Here Apple's UIKit called your [DashboardVC viewDidLoad] . So that gives a lot of context. It happend early during the construction of you view in the viewDidLoad phase.

13 UIKit 0x0000000108b49cc4 -[UIViewController loadViewIfRequired] + 1198
14 UIKit 0x0000000108b4fc7b -[UIViewController __viewWillAppear:] + 120
15 UIKit 0x0000000108b7fa37 -[UINavigationController _startCustomTransition:] + 1203
16 UIKit 0x0000000108b8fcdb -[UINavigationController _startDeferredTransitionIfNeeded:] + 712
17 UIKit 0x0000000108b90cea -[UINavigationController __viewWillLayoutSubviews] + 57
18 UIKit 0x0000000108d36c85 -[UILayoutContainerView layoutSubviews] + 248
19 UIKit 0x0000000108a6be40 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 710
20 QuartzCore 0x000000010736359a -[CALayer layoutSublayers] + 146
21 QuartzCore 0x0000000107357e70 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366
22 QuartzCore 0x0000000107357cee _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
23 QuartzCore 0x000000010734c475 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 277
24 QuartzCore 0x0000000107379c0a _ZN2CA11Transaction6commitEv + 486
25 QuartzCore 0x000000010737a37c _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 92
26 CoreFoundation 0x00000001074f5947 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION + 23
27 CoreFoundation 0x00000001074f58b7 __CFRunLoopDoObservers + 391
28 CoreFoundation 0x00000001074eb50b __CFRunLoopRun + 1147
29 CoreFoundation 0x00000001074eae08 CFRunLoopRunSpecific + 488
30 GraphicsServices 0x000000010b1b5ad2 GSEventRunModal + 161
31 UIKit 0x00000001089b730d UIApplicationMain + 171
32 Autograph24 0x0000000106b15d9f main + 111
33 libdyld.dylib 0x000000010aac192d start + 1
34 ??? 0x0000000000000001 0x0 + 1 ) libc++abi.dylib:
terminating with uncaught exception of type NSException

Combining this information I would conclude that you call [UITableView scrollToRowAtIndexPath:atScrollPosition:animated:] from within [DashboardVC viewDidLoad] , which is bound to fail with any index path because the UITableView is not properly constructed at this time of the view controller's lifecycle.

Earlier iOS versions were more lenient here; but later versions are rather restrictive about what you can do before the view controller is fully constructed.

You will need to move your [UITableView scrollToRowAtIndexPath:atScrollPosition:animated:] call to either viewWillAppear: or even viewDidAppear: .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM