未调用sizeForItemAtIndexPath和cellForItemAtIndexPath

时间:2017-06-11 19:29:50

标签: ios swift swift2.3

我试图在UICollectionViewCellCell中创建一个UICollectionView,但是没有调用func sizeForItemAtIndexPath和cellForItemAtIndexPath,这是我的代码:

import UIKit

class HistoryCollectionViewCell: UICollectionViewCell, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource{
    static let cellId = "cell"

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

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

    let appsCollectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        let collectionView = UICollectionView(frame: .zero,collectionViewLayout: layout)
        return collectionView
    }()
    func setUpCollectionView(){
        backgroundColor = UIColor.whiteColor()
        addSubview(appsCollectionView)
        appsCollectionView.delegate = self
        appsCollectionView.dataSource = self
    }
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 1
    }
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return 1
    }
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
        print("sizeForItem is called")
        return CGSizeMake(100, 100)
    }
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        print("cellForItem is called")
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath)
        return cell
    }
}

希望有人会帮忙

1 个答案:

答案 0 :(得分:1)

更新回答:

您忘记注册第二个收集视图使用的单元格:

appsCollectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "cell2")

第二,为viewcontroller设置一个有效的框架:

let frame = CGRect(x: 8, y: 8, width: 300, height: 200)
let collectionView = UICollectionView(frame: frame, collectionViewLayout: layout)

你可能会在这里混淆几个概念。为什么您的单元格实现 UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,UICollectionViewDataSource ?如果您还不关心干净的架构并且只是想测试一下,那么让CollectionView的容器视图控制器实现所述协议(可以是通用VC或子类,如UICollectionViewController)。

你所谓的 HistoryCollectionViewCell 应该是 HistoryCollectionViewController 和sublcass UIViewController或UICollectionViewController而不是UICollectionViewCell。

这些是不同的责任。您可能想要查看Github的示例或Apple的UIKit文档(例如:https://developer.apple.com/documentation/uikit/uicollectionviewdelegate)。或者在线一些教程:https://www.raywenderlich.com/136159/uicollectionview-tutorial-getting-started

class HistoryCollectionViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {
    // TODO: Refactor

    // Implement required methods
}


class HistoryCollectionViewCell: UICollectionViewCell {
    // Implement required methods
}