【问题标题】:UitableView working fine on iOS8.4 (Xcode 6.4) but crashes on iOS9.1(Xcode 7.1.1)UitableView 在 iOS8.4 (Xcode 6.4) 上运行良好,但在 iOS9.1(Xcode 7.1.1) 上崩溃
【发布时间】:2016-05-15 06:42:17
【问题描述】:

当我尝试通过 Xcode6.4 使用 UITableView 时它工作正常,但是当我通过 Xcode 7.1.1 尝试它时它崩溃了。我猜是因为iOS版本。我附上崩溃日志。请建议修复它的最佳方法是什么。我很确定: 1)数据源和代表工作正常。 2) Numberofrows 和 heightOfRow 方法也被调用,但它在 cellForRowAtIndexPath 被调用之前崩溃。

这是日志:

2016-02-05 15:34:27.400 Autograph24[3720:87023] * 断言失败 在-[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] * 终止应用 由于未捕获的异常“NSInternalInconsistencyException”,原因: '无效的索引路径' *** 第一次抛出调用堆栈:( 0 CoreFoundation 0x00000001075c9f45 exceptionPreprocess + 165 1 libobjc.A.dylib
0x0000000107956deb objc_exception_throw + 48 2 核心基础
0x00000001075c9daa +[NSException raise:format:arguments:] + 106 3
基础 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 -[仪表板VC 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 石英核心 0x0000000107357cee _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24 23 QuartzCore 0x000000010734c475 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 277 24 QuartzCore 0x0000000107379c0a _ZN2CA11Transaction6commitEv + 486 25 石英核心 0x000000010737a37c _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 92 26 核心基础 0x00000001074f5947 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION
+ 23 27 核心基础 0x00000001074f58b7 __CFRunLoopDoObservers + 391 28 CoreFoundation 0x00000001074eb50b __CFRunLoopRun + 1147 29 CoreFoundation
0x00000001074eae08 CFRunLoopRunSpecific + 488 30 图形服务
0x000000010b1b5ad2 GSEventRunModal + 161 31 UIKit
0x00000001089b730d UIApplicationMain + 171 32 Autograph24
0x0000000106b15d9f 主要 + 111 33 libdyld.dylib
0x000000010aac192d 开始 + 1 34 ???
0x0000000000000001 0x0 + 1 ) libc++abi.dylib: 终止于 NSException 类型的未捕获异常

【问题讨论】:

  • 你设置 numberOfSection 和 viewForHeader 了吗?
  • 崩溃日志中已经说明:- 由于未捕获的异常 'NSInternalInconsistencyException' 导致应用程序终止,原因:'invalid indexPath'。

标签: ios iphone xcode6 xcode7


【解决方案1】:

您的索引路径无效。这意味着该部分或行或两者都不正确。如果您有一个包含 2 个部分和每个部分 2 行的表,则 indexpath 0-3 无效,因为第 0 部分不提供第 3 行。同样,indexpath 2-1 将无效,因为第 2 部分不存在(只有 0和 1)。

至于如何解决这个问题,首先仔细检查是否调用了所有必需的方法并且您的数据已正确初始化。然后在你的类周围设置断点。主要是在您使用任何类型的索引路径的地方,并查看它尝试从您的数据外部读取的位置。

【讨论】:

    【解决方案2】:

    学习阅读崩溃日志。在大多数情况下,当然在这种情况下,它们包含所有相关信息:

    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'
    

    它清楚地表明:原因:'无效的 indexPath'

    所以第一个提示:涉及到无效的索引路径(如果您使用 UITableViews,这是众所周知的事情)。

    接下来是发生错误的堆栈跟踪。前几个堆栈位置在 Apple 的代码中。通常跳过它们。但偶尔,它们包含有趣的提示。这里有一个函数assertValidIndexPath:allowEmptySection:。这再次表明索引路径是问题所在。

    *** 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
    

    您的代码如下。看上面和下面的线。它说你的函数[DashboardVC scrollToToday] 调用了函数[UITableView scrollToRowAtIndexPath:atScrollPosition:animated:]。这是代码中触发错误的地方。

    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
    

    这是下一个有趣的位置。这里苹果的 UIKit 调用了你的[DashboardVC viewDidLoad]。因此,这提供了很多背景信息。它发生在 viewDidLoad 阶段构建你视图的早期。

    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
    

    结合这些信息,我得出的结论是,您从 [DashboardVC viewDidLoad] 内部调用了 [UITableView scrollToRowAtIndexPath:atScrollPosition:animated:],由于在视图控制器生命周期的这个时候没有正确构造 UITableView,因此任何索引路径都必然会失败。

    早期的 iO​​S 版本在这里更为宽松;但是更高版本对在视图控制器完全构建之前可以做什么有相当大的限制。

    您需要将您的 [UITableView scrollToRowAtIndexPath:atScrollPosition:animated:] 呼叫转移到 viewWillAppear: 甚至 viewDidAppear:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-15
      相关资源
      最近更新 更多