' UICollectionView必须使用非零布局参数初始化' - 斯威夫特4

时间:2017-10-08 21:08:33

标签: ios swift uicollectionview

(不要将此问题标记为重复。我阅读了很多问题,但我找不到问题的答案。)

我创建了一个IUCollectionView类和一个UICollectionViewCell类。这是集合视图之一:

class NearbyPlacesUICollectionView: UICollectionView {


    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)!
        commonInit()
        fatalError("init(coder:) has not been implemented")
    }

    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: layout)
        commonInit()
    }

    init(collectionView: UICollectionView, frame: CGRect) {
        let layout = UICollectionViewLayout()
        super.init(frame: frame, collectionViewLayout: layout)
    }

    func commonInit() {

        allowsMultipleSelection = false
        backgroundColor = .white

        let layout = UICollectionViewFlowLayout()
        layout.sectionInset = UIEdgeInsets(top: 20, left: 10, bottom: 10, right: 10)
        layout.itemSize = CGSize(width: UIScreen.main.bounds.width * 0.8875, height: 90*3)
        frame = UIScreen.main.bounds
        collectionViewLayout = layout

        register(NearbyPlacesUICollectionView.self, forCellWithReuseIdentifier: "nearbyPlaceCell")
    }
}

然后我有UIViewController我有这个(我删除了一些代码以缩短它):

    class NearbyPlacesViewController: UIViewController, UICollectionViewDataSource {

       var collectionView = NearbyPlacesUICollectionView()

       override func viewDidLoad() {
            super.viewDidLoad()

        collectionView.dataSource = self
        collectionView.delegate = self
        view.addSubview(collectionView)
       }
}

extension NearbyPlacesViewController: UICollectionViewDelegateFlowLayout, UICollectionViewDelegate {

    func numberOfSections(in tableView: UITableView) -> Int {
        return 3
    }

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

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let cell: NearbyPlacesUICollectionViewCell? = collectionView.dequeueReusableCell(withReuseIdentifier: "nearbyPlaceCell", for: indexPath) as? NearbyPlacesUICollectionViewCell

        return cell!
    }

    func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
        if let cell = cell as? NearbyPlacesUICollectionViewCell
        {
            _configureCell(cell, atIndexPath: indexPath)
        }
    }

    fileprivate func _configureCell(_ cell: NearbyPlacesUICollectionViewCell, atIndexPath indexPath: IndexPath)
    {
        cell.queue.cancelAllOperations()

        let operation: BlockOperation = BlockOperation()
        operation.addExecutionBlock { [weak operation] () -> Void in

            DispatchQueue.main.sync(execute: { [weak operation] () -> Void in

                if let operation = operation, operation.isCancelled { 
            return }

                ... // cell code here
            })
        }

        cell.queue.addOperation(operation)
    }


    func collectionView(_ collectionView: UICollectionView, viewForHeaderInSection section: Int) -> UIView? {

        let view = UIView()
        view.backgroundColor = .clear
            ... // header code here

        return view
    }
}

运行时遇到此错误:UICollectionView must be initialized with a non-nil layout parameter 我覆盖了所有init,但似乎没有调用它们。我阅读了有关类似问题的所有问题,但我没有找到任何答案。 (我使用的是Swift 4)。对不起,如果我粘贴了很多代码,但我认为这是必要的。在此先感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

问题在于这一行:

var collectionView = NearbyPlacesUICollectionView()

这最终调用了错误的init方法,其中没有设置布局。您已在init课程中定义了3种NearbyPlacesUICollectionView方法。使用导致布局设置的两个中的一个。

这个init方法:

init(collectionView: UICollectionView, frame: CGRect)

毫无意义。它应该是:

init(frame: CGRect)

因为您不使用(并且绝对不需要)collectionView属性。

这个init

也很奇怪
override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout)

调用您的commonInit,然后创建并设置布局。但是您已经将布局设置为传递给init的布局。