[英]UiCollectionView must be initialized with a non-nil layout parameter Swift 3
I have searched StackOverflow for answers to this and have tried different ways of implementing this. 我已经在StackOverflow上搜索了此问题的答案,并尝试了不同的实现方法。 I have a collectionView
that has a button that, when pressed, attempts to initialize another collectionView
. 我有一个collectionView
,它具有一个按钮,当按下该按钮时,它会尝试初始化另一个collectionView
。 Here's the stack trace: 这是堆栈跟踪:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'UICollectionView must be initialized with a non-nil layout parameter'
*** First throw call stack:
(
0 CoreFoundation 0x000000010adb3d4b __exceptionPreprocess + 171
1 libobjc.A.dylib 0x00000001079b321e objc_exception_throw + 48
2 CoreFoundation 0x000000010ae1d2b5 +[NSException raise:format:] + 197
3 UIKit 0x0000000108eca775 -[UICollectionView initWithFrame:collectionViewLayout:] + 76
4 UIKit 0x0000000108f13abb -[UICollectionViewController _newCollectionViewWithFrame:collectionViewLayout:] + 108
5 UIKit 0x0000000108f12c94 -[UICollectionViewController loadView] + 678
6 UIKit 0x000000010873761c -[UIViewController loadViewIfRequired] + 201
7 UIKit 0x0000000108737e70 -[UIViewController view] + 27
8 UIKit 0x0000000108ff86a4 -[_UIFullscreenPresentationController _setPresentedViewController:] + 87
9 UIKit 0x0000000108712702 -[UIPresentationController initWithPresentedViewController:presentingViewController:] + 141
10 UIKit 0x000000010874ae97 -[UIViewController _presentViewController:withAnimationController:completion:] + 3956
11 UIKit 0x000000010874e26b -[UIViewController _performCoordinatedPresentOrDismiss:animated:] + 530
12 UIKit 0x000000010874dd51 -[UIViewController presentViewController:animated:completion:] + 179
13 TheAppProject 0x0000000107385842 _TFC17TheAppProject15DailyController12publishedTapfT6senderCSo8UIButton_T_ + 754
14 TheAppProject 0x00000001073859fa _TToFC17TheAppProject15DailyController12publishedTapfT6senderCSo8UIButton_T_ + 58
15 UIKit 0x00000001085978bc -[UIApplication sendAction:to:from:forEvent:] + 83
16 UIKit 0x000000010871dc38 -[UIControl sendAction:to:forEvent:] + 67
17 UIKit 0x000000010871df51 -[UIControl _sendActionsForEvents:withEvent:] + 444
18 UIKit 0x000000010871ce4d -[UIControl touchesEnded:withEvent:] + 668
19 UIKit 0x0000000108605545 -[UIWindow _sendTouchesForEvent:] + 2747
20 UIKit 0x0000000108606c33 -[UIWindow sendEvent:] + 4011
21 UIKit 0x00000001085b39ab -[UIApplication sendEvent:] + 371
22 UIKit 0x0000000108da072d __dispatchPreprocessedEventFromEventQueue + 3248
23 UIKit 0x0000000108d99463 __handleEventQueue + 4879
24 CoreFoundation 0x000000010ad58761 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
25 CoreFoundation 0x000000010ad3d98c __CFRunLoopDoSources0 + 556
26 CoreFoundation 0x000000010ad3ce76 __CFRunLoopRun + 918
27 CoreFoundation 0x000000010ad3c884 CFRunLoopRunSpecific + 420
28 GraphicsServices 0x000000010dc1ba6f GSEventRunModal + 161
29 UIKit 0x0000000108595c68 UIApplicationMain + 159
30 TheAppProject 0x0000000107366d8f main + 111
31 libdyld.dylib 0x000000010bd6368d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
Here's my code: 这是我的代码:
private let reuseIdentifier = "cell"
class MyCollectionViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
let myCell = MyCell()
override func viewDidLoad() {
super.viewDidLoad()
let layout = UICollectionViewFlowLayout()
let collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
collectionView.backgroundColor = UIColor.black
collectionView.alwaysBounceVertical = true
collectionView.delegate = self
collectionView.dataSource = self
self.collectionView!.register(MyCell.self, forCellWithReuseIdentifier: reuseIdentifier)
}
// MARK: - Layout
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: 325, height: 630)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 60.0
}
}
Your MyCollectionViewController class is a subclass of UICollectionViewController which has a property of collectionView which is the actual UICollectionView itself. 您的MyCollectionViewController类是UICollectionViewController的子类,它具有collectionView的属性,该属性是实际的UICollectionView本身。 So the separate UICollectionView you create in this line: 因此,您在此行中创建了单独的UICollectionView:
let collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
is not the same as self.collectionView which is the one provided by the UICollectionViewController itself. 与UICollectionViewController本身提供的self.collectionView不同。
So this is not the source of the error you are getting. 因此,这不是您得到的错误的根源。
In fact the error you are seeing is relayed to how you are creating and presenting the MyCollectionViewController itself. 实际上,您所看到的错误将中继到您如何创建和呈现MyCollectionViewController本身。 You don't show the code for how that is created but you are not passing any kind of layout to it. 您没有显示代码的创建方式,但没有向其传递任何布局。 For example to create it in code you would do this: 例如,要在代码中创建它,您可以这样做:
let layout = UICollectionViewFlowLayout()
let myCollectionVC = MyCollectionViewController(collectionViewLayout: layout)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.