单元格未出现在UICollectionView中

时间:2017-06-17 10:25:07

标签: swift uicollectionview uicollectionviewcell

我不明白为什么我的细胞没有出现:

class MenuBar: UIView, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {

    let cellId = "pouet"

    lazy var customCollectionView : UICollectionView = {
        let layout = UICollectionViewLayout()
        let cv  = UICollectionView(frame: .zero, collectionViewLayout: layout)
        cv.backgroundColor = UIColor.gray
        cv.dataSource = self
        cv.delegate = self

        return cv
    }()

    override init(frame: CGRect) {
        super.init(frame: frame)

        customCollectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: cellId)
        addSubview(customCollectionView)
        addConstraintsWithFormat(format: "H:|[v0]|", views: customCollectionView)
        addConstraintsWithFormat(format: "V:|[v0]|", views: customCollectionView)
    }

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

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = customCollectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath)
        print("test")

        return cell
    }

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

现在我的print("test")也没有显示,这意味着该方法实际上没有被调用,对吧?那有什么不对?

1 个答案:

答案 0 :(得分:5)

更改此行代码:

DownloadFile

要:

let layout = UICollectionViewLayout()

UICollectionViewLayout是抽象类,你不能直接使用它。