【问题标题】:collectionview scrollToItemAtIndexPath crashes appcollectionview scrollToItemAtIndexPath 使应用程序崩溃
【发布时间】:2017-05-10 18:02:35
【问题描述】:

所以我在我的 viewDidLoad 函数中放置了以下代码,它使应用程序崩溃。任何想法为什么会发生这种情况或解决这个问题的方法?

self.collectionView?.scrollToItem(at: IndexPath(row: 0, section: 0), at: .top, animated: true)

---------------编辑--------------- 添加错误日志

2016-12-26 18:40:08.354 ParseStarterProject-Swift[73829:1561943] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'attempt to scroll to invalid index path: <NSIndexPath: 0x608000226400> {length = 2, path = 0 - 0}'
*** First throw call stack:
(
    0   CoreFoundation                      0x00000001066e7d4b __exceptionPreprocess + 171
    1   libobjc.A.dylib                     0x000000010614921e objc_exception_throw + 48
    2   CoreFoundation                      0x00000001067512b5 +[NSException raise:format:] + 197
    3   UIKit                               0x0000000107fb0e44 -[UICollectionView scrollToItemAtIndexPath:atScrollPosition:animated:] + 224
    4   ParseStarterProject-Swift           0x00000001051a963c _TFC25ParseStarterProject_Swift18PackViewController13viewDidAppearfSbT_ + 700
    5   ParseStarterProject-Swift           0x00000001051a9741 _TToFC25ParseStarterProject_Swift18PackViewController13viewDidAppearfSbT_ + 49
    6   UIKit                               0x0000000107808a6c -[UIViewController _setViewAppearState:isAnimating:] + 945
    7   UIKit                               0x0000000107809388 -[UIViewController _endAppearanceTransition:] + 197
    8   UIKit                               0x00000001077de28d -[UIPresentationController transitionDidFinish:] + 834
    9   UIKit                               0x00000001079f5f83 -[_UICurrentContextPresentationController transitionDidFinish:] + 42
    10  UIKit                               0x00000001077e1ef0 __56-[UIPresentationController runTransitionForCurrentState]_block_invoke_2 + 183
    11  UIKit                               0x00000001081a356c -[_UIViewControllerTransitionContext completeTransition:] + 102
    12  UIKit                               0x00000001077daddc -[UITransitionView notifyDidCompleteTransition:] + 251
    13  UIKit                               0x00000001077daaef -[UITransitionView _didCompleteTransition:] + 1539
    14  UIKit                               0x00000001077dd51c -[UITransitionView _transitionDidStop:finished:] + 104
    15  UIKit                               0x00000001076edbd5 -[UIViewAnimationState sendDelegateAnimationDidStop:finished:] + 222
    16  UIKit                               0x00000001076ee12a -[UIViewAnimationState animationDidStop:finished:] + 136
    17  QuartzCore                          0x0000000107392648 _ZN2CA5Layer23run_animation_callbacksEPv + 316
    18  libdispatch.dylib                   0x00000001098940cd _dispatch_client_callout + 8
    19  libdispatch.dylib                   0x00000001098748a4 _dispatch_main_queue_callback_4CF + 406
    20  CoreFoundation                      0x00000001066abe49 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
    21  CoreFoundation                      0x000000010667137d __CFRunLoopRun + 2205
    22  CoreFoundation                      0x0000000106670884 CFRunLoopRunSpecific + 420
    23  GraphicsServices                    0x000000010bf76a6f GSEventRunModal + 161
    24  UIKit                               0x0000000107660c68 UIApplicationMain + 159
    25  ParseStarterProject-Swift           0x00000001051966cf main + 111
    26  libdyld.dylib                       0x00000001098e068d start + 1
    27  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

【问题讨论】:

  • 你能提供更多细节吗?可以添加崩溃日志吗?
  • 多半是因为你还没有加载Collection View,在collection view加载成功后尝试移动这段代码。另外,请同时发布错误消息。

标签: ios swift xcode uicollectionview swift3


【解决方案1】:

可能的问题

您的collectionView 数据源是否有任何数据,如果为零,numberOfItemsInSection 将返回 0,并且因为您要求 collectionView 滚动到 indexPath IndexPath(row: 0, section: 0) 并且因为单元格不存在它可能会崩溃。

解决方案

  1. 确保您的数据源不为零或始终返回至少一个单元格

  2. collectionView 重新加载数据后,滚动到 viewDidAppear 中的指定 indexPath(假设在调用 viewDidAppear 时您将拥有一些数据)

解决方法

在要求 collectionView 滚动到指定的 indexPath 之前,询问您的数据源是否在 indexPath(0,0) 处有一个单元格

if let _ = self.collectionView?.dataSource?.collectionView(self.collectionView!, cellForRowAt: IndexPath(row: 0, section: 0)) {
    self.collectionView?.scrollToItem(at: IndexPath(row: 0, section: 0), at: .top, animated: true)
}

编辑:

正如您提到的数据来自服务器,正确的解决方案是在收到网络响应后重新加载 collectionView。

在 webservice 调用的完成块中重新加载 collectionView,然后滚动到特定的索引路径。

如果您使用的是 alamo fire 或 AFNetworking,则在主线程上调用完成块,以防您的完成块在后台线程中执行,请确保在重新加载和滚动之前切换到主线程:)

希望对您有所帮助 :) 编码愉快 :)

【讨论】:

  • 嘿桑迪普,我也在 viewdidappear 函数中尝试过,结果相同。可能是因为数据来自服务器,但它应该在那里,因为它确实显示。我喜欢你的想法或在执行之前检查有效数据但是,collectionview 中没有 tableview 选项。
  • 哈哈哈哈哈哈抱歉打错了:D
  • 我实际上在使用 parse,但感谢您的提醒,看看它是有道理的。
【解决方案2】:

这次崩溃可能有很多原因。您可以通过添加多个检查来阻止这种情况,就像 1.查看CollectionView数据 2.等待0.5秒,首先完全加载CollectionView并滚动它。

【讨论】:

  • 现在向调度队列添加一个计时器是一个非常简单的解决方法。
  • 像这样添加计时器总是一个坏主意,因为它会使应用程序不可预测(例如,如果由于某种原因需要超过 0.5 秒怎么办)
【解决方案3】:

我正在执行此检查以防止我的应用程序崩溃。奇怪的是,当崩溃发生时,确实有一个可见的单元格,但尝试访问它会返回 nil。

if let _ = collectionView.cellForItem(at: IndexPath(row: 0, section: 0)) {
    collectionView.scrollToItem(at: IndexPath(item: 0, section: 0), at: .top, animated: true)
}

【讨论】:

    【解决方案4】:

    尝试在 self.collectionView?.scrollToItem 之前调用

    collectionView.reloadData()
    

    【讨论】:

      【解决方案5】:

      您可以尝试使用like:

      DispatchQueue.main.async {
         self.collectionView?.scrollToItem(at: IndexPath(row: 0, section: 0), at: .top, animated: true)
      }
      

      【讨论】:

      • 我已经在 dispatchcue 中尝试过,因为我重新加载数据,viewwillappear,viewdidappear 他们似乎都产生了小错误
      • ...不要这样做...期间
      【解决方案6】:

      也许试试这个?

      let indexToScroll = 2
      if let itemsCount = collectionView.dataSource?.collectionView(collectionView, numberOfItemsInSection: 0), indexToScroll < itemsCount {
          collectionView.scrollToItem(at: IndexPath(row: indexToScroll, section: 0), at: .centeredHorizontally, animated: true)
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-08-23
        • 2017-11-10
        • 2012-08-18
        • 2012-02-12
        • 2014-03-18
        相关资源
        最近更新 更多