Ios collectionview scrollToItemAtIndexPath应用程序崩溃
因此,我在viewDidLoad函数中放置了以下代码,它会使应用程序崩溃。你知道为什么会发生这种情况,或者有什么办法吗Ios collectionview scrollToItemAtIndexPath应用程序崩溃,ios,swift,xcode,uicollectionview,swift3,Ios,Swift,Xcode,Uicollectionview,Swift3,因此,我在viewDidLoad函数中放置了以下代码,它会使应用程序崩溃。你知道为什么会发生这种情况,或者有什么办法吗 self.collectionView?.scrollToItem(at: IndexPath(row: 0, section: 0), at: .top, animated: true) ---------------编辑--------------- 添加了错误日志 2016-12-26 18:40:08.354 ParseStarterProject-Swift[738
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)
2016-12-26 18:40:08.354 ParseStarter项目Swift[73829:1561943]***由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“尝试滚动到无效的索引路径:{length=2,path=0-0}”
***第一次抛出调用堆栈:
(
0 CoreFoundation 0x00000001066e7d4b例外预处理+171
1 libobjc.A.dylib 0x000000010614921e objc_异常_抛出+48
2 CoreFoundation 0x00000001067512b5+[N异常提升:格式:][197
3 UIKit 0x0000000107fb0e44-[UICollectionView scrollToItemAtIndexPath:atScrollPosition:animated:+224
4项目Swift 0x00000001051a963c\u TFC25ParseStarterProject\u SWIFT18PACKVIEWCONTROLLER 13VIEWDIDAPEARFSBT+700
5 ParseStarter项目Swift 0x00000001051a9741 _TTOFC25ParseStarter项目_SWIFT18PackViewController13视图显示FSBT+49
6 UIKit 0x0000000107808a6c-[UIViewController\u setViewAppearState:isAnimating:+945
7 UIKit 0x0000000107809388-[UIViewController\u endAppearanceTransition::+197
8 UIKit 0x00000001077de28d-[UIPresentationController转换IDFinish:+834
9 UIKit 0x00000001079f5f83-[\u UICurrentContextPresentationController转换IDFinish:+42
10 UIKit 0x00000001077e1ef0 \uu 56-[UIPresentationController runTransitionForCurrentState]\u块\u调用\u 2+183
11 UIKit 0x00000001081a356c-[\u UIViewControllerTransitionContext completeTransition:][102
12 UIKit 0x00000001077daddc-[UITransitionView notifyDidCompleteTransition:][251
13 UIKit 0x00000001077daaef-[UITransitionView\u didCompleteTransition:][1539
14 UIKit 0x00000001077dd51c-[UITransitionView\u TransitiondStop:finished:][104
15 UIKit 0x00000001076edbd5-[UIViewAnimationState sendDelegateAnimationDidStop:finished:+222
16 UIKit 0x00000001076ee12a-[UIViewAnimationState animationDidStop:finished:][136
17 QuartzCore 0x0000000107392648_ZN2CA5layer23运行_动画_callbacksEPv+316
18 libdispatch.dylib 0x00000001098940cd _dispatch_client_callout+8
19 libdispatch.dylib 0x00000001098748a4_dispatch_main_queue_callback_4CF+406
20 CoreFoundation 0x00000001066abe49\uuuu CFRUNLOOP\u正在维护\u主\u调度\u队列\uuu9
21 CoreFoundation 0x000000010667137d\uuu CFRunLoopRun+2205
22 CoreFoundation 0x0000000106670884 CFRunLoopRunSpecific+420
23图形服务0x000000010bf76a6f GSEventRunModal+161
24 UIKit 0x0000000107660c68 UIApplicationMain+159
25项目Swift 0x00000001051966cf干管+111
26 libdyld.dylib 0x00000001098e068d启动+1
27°?0x0000000000000001 0x0+1
)
libc++abi.dylib:以NSException类型的未捕获异常终止
(lldb)
可能的问题
您的collectionView
数据源是否有任何数据,如果该数据为零,numberofitemsinssection
将返回0,并且因为您要求collectionView
滚动到indexPathindexPath(行:0,节:0)
并且因为单元格不在那里,它可能会崩溃
解决方案
ViewDidDisplay
中指定的indexPath(假设调用ViewDidDisplay
时您将拥有一些数据)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)
}
编辑:
正如您提到的,数据来自服务器,正确的解决方案是在收到web响应后重新加载collectionView
在webservice调用的完成块中,重新加载collectionView,然后滚动到特定的索引路径
如果您正在使用alamo fire或AFNetworking,则会在主线程上调用完成块,如果在后台线程中执行完成块,请确保在重新加载和滚动之前切换到主线程:)
希望它有帮助:)快乐编码:)您能尝试使用类似的:
DispatchQueue.main.async {
self.collectionView?.scrollToItem(at: IndexPath(row: 0, section: 0), at: .top, animated: true)
}
这次崩溃可能有很多原因。您可以通过添加多个检查来停止此操作,就像 1.检查CollectionView数据
2.等待0.5秒,首先完全加载CollectionView并滚动它。我正在进行此检查以防止应用程序崩溃。奇怪的是,当崩溃发生时,确实有一个可见的单元格,但尝试访问它时返回零
if let _ = collectionView.cellForItem(at: IndexPath(row: 0, section: 0)) {
collectionView.scrollToItem(at: IndexPath(item: 0, section: 0), at: .top, animated: true)
}
尝试在self.collectionView?之前调用scrollToItem
collectionView.reloadData()
也许试试这个
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)
}
让索引滚动=2
如果让itemsCount=collectionView.dataSource?.collectionView(collectionView,numberOfItemsInSection:0),indexToScroll
您能提供更多详细信息吗?你能添加崩溃日志吗?主要是因为你还没有加载Co