gpt4 book ai didi

ios - UiCollectionView 必须使用非 nil 布局参数初始化 Swift 3

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

我在 StackOverflow 上搜索了这个问题的答案,并尝试了不同的实现方法。我有一个 collectionView,它有一个按钮,当按下该按钮时,它会尝试初始化另一个 collectionView。这是堆栈跟踪:

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

这是我的代码:

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

最佳答案

您的 MyCollectionViewController 类是 UICollectionViewController 的子类,它具有 collectionView 的属性,即实际的 UICollectionView 本身。因此,您在此行中创建的单独 UICollectionView:

let collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)

与 self.collectionView 不同,self.collectionView 是由 UICollectionViewController 本身提供的。

所以这不是您收到的错误的来源。

事实上,您所看到的错误会传递给您创建和呈现 MyCollectionViewController 本身的方式。您没有显示有关如何创建它的代码,但您没有向它传递任何类型的布局。例如,要在代码中创建它,您可以这样做:

let layout = UICollectionViewFlowLayout()
let myCollectionVC = MyCollectionViewController(collectionViewLayout: layout)

关于ios - UiCollectionView 必须使用非 nil 布局参数初始化 Swift 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44358118/

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