简体   繁体   English

'UICollectionView必须使用非nil布局参数初始化'-Swift 4

[英]'UICollectionView must be initialized with a non-nil layout parameter' - Swift 4

(Don't mark this question as duplicated. I read a lot of questions but I don't find the answer to my issue.) (不要将此问题标记为重复。我读了很多问题,但是找不到我问题的答案。)

I have created a IUCollectionView class and a UICollectionViewCell class. 我创建了IUCollectionView类和UICollectionViewCell类。 Here is the collection view one: 这是集合视图之一:

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

Then I have a UIViewController where I have this (I remove some code to make it shorter): 然后,我在其中有一个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
    }
}

I get this error when running: UICollectionView must be initialized with a non-nil layout parameter I override all the init but it seems like they are not called. 运行时出现此错误: UICollectionView must be initialized with a non-nil layout parameter我会覆盖所有init,但似乎未调用它们。 I read all questions about similar issues but I didn't find any answer. 我阅读了有关类似问题的所有问题,但没有找到任何答案。 (I'm using Swift 4). (我正在使用Swift 4)。 Sorry If I paste a lot of code but I think it's necessary. 抱歉,如果我粘贴很多代码,但是我认为这是必要的。 Thanks in advance for your help. 在此先感谢您的帮助。

The problem is this line: 问题是这一行:

var collectionView = NearbyPlacesUICollectionView()

This ends up calling the wrong init method where no layout is set. 最终将导致调用错误的init方法(未设置布局)。 You've defined 3 init methods in your NearbyPlacesUICollectionView class. 您已经在NearbyPlacesUICollectionView类中定义了3个init方法。 Use one of the two that causes the layout to be set. 使用导致设置布局的两种方法之一。

And this init method: 这个init方法:

init(collectionView: UICollectionView, frame: CGRect)

makes no sense. 没有意义。 It should be: 它应该是:

init(frame: CGRect)

since you don't use (and definitely don't need) the collectionView property. 因为您不使用(而且绝对不需要) collectionView属性。

It's also odd that this init : 这个init也很奇怪:

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

calls your commonInit which in turn creates and set a layout. 调用您的commonInit ,后者依次创建并设置布局。 But you've already set the layout to the one passed to the init . 但是您已经将布局设置为传递给init的布局。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 UiCollectionView必须使用非nil布局参数初始化Swift 3 - UiCollectionView must be initialized with a non-nil layout parameter Swift 3 即使在UICollectionView文件中定义了参数,也必须在Swift中使用非nil布局参数初始化UICollectionView - UICollectionView must be initialized with a non-nil layout parameter in Swift, even though the parameter is defined in the UICollectionView file 必须使用非nil布局参数初始化UICollectionView。 迅速与UICollectionView错误 - UICollectionView must be initialized with a non-nil layout parameter. error in swift with UICollectionView “ UICollectionView必须使用非null布局参数初始化” - 'UICollectionView must be initialized with a non-nil layout parameter' 错误:UICollectionView必须使用非nil布局参数初始化 - Error: UICollectionView must be initialized with a non-nil layout parameter UICollectionView:必须使用非 nil 布局参数初始化 - UICollectionView: must be initialized with a non-nil layout parameter “必须使用非零布局参数初始化 UICollectionView”错误 - 'UICollectionView must be initialized with a non-nil layout parameter' bug 由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'UICollectionView必须使用非nil布局参数初始化 - Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'UICollectionView must be initialized with a non-nil layout parameter 使用JSQMessagesCollectionView时,“ UICollectionView必须使用非nil布局参数初始化” - 'UICollectionView must be initialized with a non-nil layout parameter' while using JSQMessagesCollectionView Swift 3.0 中的 UICollectionViewController 错误:必须使用非 nil 布局参数进行初始化 - UICollectionViewController Error in Swift 3.0: must be initialized with a non-nil layout parameter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM