Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/117.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios ';UICollectionView必须使用非nil布局参数初始化';-斯威夫特4_Ios_Swift_Uicollectionview - Fatal编程技术网

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

Ios ';UICollectionView必须使用非nil布局参数初始化';-斯威夫特4,ios,swift,uicollectionview,Ios,Swift,Uicollectionview,(不要将此问题标记为重复。我读了很多问题,但没有找到问题的答案。) 我创建了一个IUCollectionView类和一个UICollectionViewCell类。以下是一个集合视图: class NearbyPlacesUICollectionView: UICollectionView { required init(coder aDecoder: NSCoder) { super.init(coder: aDecoder)! commonInit

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

我创建了一个
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
,在那里我有这个(我删除了一些代码以使它更短):

运行时出现此错误:
UICollectionView必须使用非nil布局参数初始化

我覆盖了所有的init,但似乎它们没有被调用。我阅读了所有关于类似问题的问题,但没有找到任何答案。(我用的是Swift 4)。很抱歉,如果我粘贴了很多代码,但我认为这是必要的。提前感谢您的帮助。

问题在于这一行:

var collectionView = NearbyPlacesUICollectionView()
这会在没有设置布局的情况下调用错误的
init
方法。您已经在
NearbyPlacesUICollectionView
类中定义了3个
init
方法。使用导致设置布局的两个选项之一

这个
init
方法:

init(collectionView: UICollectionView, frame: CGRect)
毫无意义。应该是:

init(frame: CGRect)
因为您不使用(而且绝对不需要)collectionView属性

同样奇怪的是,这个
init

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

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

的布局。问题是这一行:

var collectionView = NearbyPlacesUICollectionView()
这会在没有设置布局的情况下调用错误的
init
方法。您已经在
NearbyPlacesUICollectionView
类中定义了3个
init
方法。使用导致设置布局的两个选项之一

这个
init
方法:

init(collectionView: UICollectionView, frame: CGRect)
毫无意义。应该是:

init(frame: CGRect)
因为您不使用(而且绝对不需要)collectionView属性

同样奇怪的是,这个
init

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

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

的布局,那么我只需要这个init:
覆盖init(frame:CGRect,collectionViewLayout:UICollectionViewLayout)
。但是如果我删除我的
commonInit()
调用,我可以在哪里设置布局?谢谢。如果您使用该
init
,则将所需布局传递给
init
。好的,那么我只需要此init:
覆盖init(frame:CGRect,collectionViewLayout:UICollectionViewLayout)
。但是如果我删除我的
commonInit()
调用,我可以在哪里设置布局?谢谢。如果您使用该
init
,则将所需布局传递给
init