簡體   English   中英

UiCollectionView必須使用非nil布局參數初始化Swift 3

[英]UiCollectionView must be initialized with a non-nil layout parameter Swift 3

我已經在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)

與UICollectionViewController本身提供的self.collectionView不同。

因此,這不是您得到的錯誤的根源。

實際上,您所看到的錯誤將中繼到您如何創建和呈現MyCollectionViewController本身。 您沒有顯示代碼的創建方式,但沒有向其傳遞任何布局。 例如,要在代碼中創建它,您可以這樣做:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM