如何从swift中的代码设置集合视图的单元格大小?

时间:2017-08-30 16:14:11

标签: ios swift uicollectionview

以下代码说明了我的类“MainVC”无法从UICollectionViewFlowLayout继承的错误,sizeForItemAt也没有自动完成,我错过了什么?我需要创建一个大小为屏幕宽度一半的单元格

extension HomeVC:UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewFlowLayout {
    func collectionView(_ collectionView: UICollectionView,numberOfItemsInSection section: Int) -> Int { return subjectList.count }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "subject", for: indexPath) as! SubjectCell
        let subject = subjectList[indexPath.row]as Subjects
        cell.subject = subject
        return cell
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
    {
        return CGSize(width: UIScreen.main.bounds.width/2, height: 100.0)
    }
}

1 个答案:

答案 0 :(得分:1)

符合UICollectionViewDelegateFlowLayout而不是UICollectionViewFlowLayout