“ UICollectionView必须使用非nil布局参数初始化”吗?

时间:2019-04-26 22:48:02

标签: swift layout uicollectionview

我试图编写一个自定义类来创建UICollectionView,我打算从不同的类中创建多个实例。下面是我在自定义UICollectionView类中使用的代码:

import UIKit

class CustomCollectionView: UICollectionView, UICollectionViewDelegate, UICollectionViewDataSource {



    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {

        super.init(frame: frame, collectionViewLayout: layout)

        setupCollectionView(topEdgeInset: 20, bottomEdgeInset: 20, leftEdgeInset: 20, rightEdgeInset: 20, screenWdith: frame.width, screenHeight: frame.height, minimumCellsHorizontalSpacing: 20, minimumCellsVerticalSpacing: 20, numberOfCellsPerRow: 2, viewCollectionViewWillBeAddedTo: self, dataSource: self as! UICollectionViewDataSource, delegate: self as! UICollectionViewDelegate)

    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    convenience init(topEdgeInset: CGFloat, bottomEdgeInset: CGFloat, leftEdgeInset: CGFloat, rightEdgeInset: CGFloat, screenWdith: CGFloat, screenHeight: CGFloat, minimumCellsHorizontalSpacing: CGFloat, minimumCellsVerticalSpacing: CGFloat, numberOfCellsPerRow: CGFloat, viewCollectionViewWillBeAddedTo: UIView, dataSource: UICollectionViewDataSource, delegate: UICollectionViewDelegate) {

        self.init(frame: viewCollectionViewWillBeAddedTo.bounds,collectionViewLayout: UICollectionViewFlowLayout())

        setupCollectionView(topEdgeInset: topEdgeInset, bottomEdgeInset: bottomEdgeInset, leftEdgeInset: leftEdgeInset, rightEdgeInset: rightEdgeInset, screenWdith: screenWdith, screenHeight: screenHeight, minimumCellsHorizontalSpacing: minimumCellsHorizontalSpacing, minimumCellsVerticalSpacing: minimumCellsVerticalSpacing, numberOfCellsPerRow: numberOfCellsPerRow, viewCollectionViewWillBeAddedTo: viewCollectionViewWillBeAddedTo, dataSource: dataSource, delegate: delegate)

    }

    func setupCollectionView (topEdgeInset topInset: CGFloat, bottomEdgeInset bottomInset: CGFloat, leftEdgeInset leftInset: CGFloat, rightEdgeInset rightInset: CGFloat, screenWdith width: CGFloat, screenHeight height: CGFloat, minimumCellsHorizontalSpacing cellsHorizontalSpacing: CGFloat, minimumCellsVerticalSpacing cellsVerticalSpacing: CGFloat, numberOfCellsPerRow cellsPerRow: CGFloat, viewCollectionViewWillBeAddedTo hostView: UIView, dataSource: UICollectionViewDataSource, delegate: UICollectionViewDelegate) {

        let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()

        layout.sectionInset = UIEdgeInsets(top: topInset, left: leftInset, bottom: bottomInset, right: rightInset)

        layout.minimumLineSpacing = cellsVerticalSpacing

        layout.minimumInteritemSpacing = cellsHorizontalSpacing

        let widthOfCollectionViewCell: CGFloat = width - (leftInset + rightInset + cellsHorizontalSpacing) / cellsPerRow

        layout.itemSize = CGSize(width: widthOfCollectionViewCell, height: 100)

        let myCollectionView = UICollectionView(frame: hostView.bounds, collectionViewLayout: layout)

        myCollectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "MyCell")

        myCollectionView.dataSource = dataSource

        myCollectionView.delegate = delegate

        hostView.addSubview(self)

    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 8
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let myCell = collectionView.dequeueReusableCell(withReuseIdentifier: "MyCell", for: indexPath)

        myCell.backgroundColor = .blue

        return myCell
    }

}

下面的类是我从上面的自定义UICollectionView类创建实例的地方:

class FirstItemInTabBarOpenRolledSections: UIViewController
{

    override func viewDidLoad() {

        super.viewDidLoad()

        view.backgroundColor = .black

    }

    override func viewWillAppear(_ animated: Bool) {

        super.viewWillAppear(animated)
_ = CustomCollectionView(topEdgeInset: 20, bottomEdgeInset: 20, leftEdgeInset: 20, rightEdgeInset: 20, screenWdith: self.view.frame.width, screenHeight: self.view.frame.height, minimumCellsHorizontalSpacing: 20, minimumCellsVerticalSpacing: 20, numberOfCellsPerRow: 2, viewCollectionViewWillBeAddedTo: self.view, dataSource: self as! UICollectionViewDataSource, delegate: self as! UICollectionViewDelegate)

    }
func numberOfSections(in collectionView: UICollectionView) -> Int {

        return 1

    }


    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

        print("User tapped on itme \(indexPath.row)")

    }

}

但是,每次我运行该应用程序时,它都会崩溃,并显示一条错误消息:

  

由于未捕获的异常“ NSInvalidArgumentException”而终止应用程序,原因:“ UICollectionView必须使用非空布局参数初始化”

我不了解我的布局未初始化?有人能指出问题出在哪里吗?

1 个答案:

答案 0 :(得分:0)

错误已清除。创建集合视图时需要指定布局,但不必这样做。

为方便起见init,您致电self.init(),但必须致电self.init(frame:,collectionViewLayout:)

作为正确方法的一部分,您需要重构setupCollectionView方法。