必须使用非NIL布局参数Swift初始化UICollectionView



我已经搜索了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不同,它是UicollectionViewController本身提供的。

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

实际上,您看到的错误传达到您如何创建和介绍mycollectionViewController本身。您没有显示如何创建的代码,但是您没有将任何形式的布局传递给它。例如,要在代码中创建它,您将执行此操作:

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

最新更新