gpt4 book ai didi

ios - collectionview scrollToItemAtIndexPath 崩溃应用程序

转载 作者:可可西里 更新时间:2023-11-01 00:02:13 25 4
gpt4 key购买 nike

所以我在我的 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)

最佳答案

可能的问题

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

解决方案

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

  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 调用的完成 block 中重新加载 collectionView,然后滚动到特定的索引路径。

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

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

关于ios - collectionview scrollToItemAtIndexPath 崩溃应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41328421/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com