首页 > 解决方案 > CollectionViewCell 中 CollectionView 的数据源始终为 nil

问题描述

我有一个UICollectionView里面的一个UICollectionViewCell,和一个单独的NSObject那个是dataSource。我可以dataSource为外部设置UICollectionView,但不能为内部设置。

这是包含 internal 的单元格UICollectionView

class FeaturedCell: UICollectionViewCell, UICollectionViewDelegate {

    @IBOutlet var collectionView: UICollectionView!
    let data = FeaturedData()

    override init(frame: CGRect) {
        super.init(frame: frame)
        //setUp()
    }

    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }

    func setUp() {
        collectionView.dataSource = data
        collectionView.delegate = self
        collectionView.reloadData()

    }
}

extension FeaturedCell {

    func setCollectionViewDataSourceDelegate <D: FeaturedData> (_ dataSourceDelegate: D, forRow row: Int) {

        collectionView.delegate = dataSourceDelegate
        collectionView.dataSource = dataSourceDelegate

        collectionView.reloadData()
        print("Reload Data")

    }

}

并且UIView包含外部UICollectionView

class MainView: UIView, UICollectionViewDelegate {

    @IBOutlet var collectionView: UICollectionView!
    let data = MainData()

    override func awakeFromNib() {
        setUp()
    }


    func setUp() {
        collectionView.dataSource = data
        collectionView.delegate = self
        collectionView.backgroundColor = UIColor.orange
        collectionView.collectionViewLayout = createLayout()
        collectionView.isPagingEnabled = true
        collectionView.bounces = false
        collectionView.allowsSelection = true
    }

    func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
        print("WillDisplay")
        guard let cell: FeaturedCell = collectionView.dequeueReusableCell(withReuseIdentifier: "FeaturedCell", for: indexPath) as? FeaturedCell else {
            fatalError("Unable to dequeue FeaturedCell.")
        }
        cell.setCollectionViewDataSourceDelegate(featuredData, forRow: indexPath.item)
    }
}

这两种方法都被调用了,但从未设置dataSourceand 。delegates我也完全按照教程进行操作(使用UITableView, 甚至),但它仍然不会设置dataSourceor delegate。我究竟做错了什么?

标签: iosswiftxcodeuitableviewuicollectionview

解决方案


我认为这会有所帮助

class FeaturedCell: UICollectionViewCell {

    @IBOutlet var collectionView: UICollectionView!

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

    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }

}

extension FeaturedCell {

    func setCollectionViewDataSourceDelegate <T: UICollectionViewDelegate , D: FeaturedData> (delegate: T  dataSource: D, forRow row: Int) {

        collectionView.delegate = delegate
        collectionView.dataSource = dataSource

        collectionView.reloadData()
        print("Reload Data")
    }
}

在主视图中

func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
    print("WillDisplay")
    guard let cell: FeaturedCell = collectionView.dequeueReusableCell(withReuseIdentifier: "FeaturedCell", for: indexPath) as? FeaturedCell else {
        fatalError("Unable to dequeue FeaturedCell.")
    }
    cell.setCollectionViewDataSourceDelegate(self, featuredData, forRow: indexPath.item)
}

对于本教程,您遵循此教程的Github 链接,将您的代码与该链接进行比较,看看您错过了哪里。

希望这会有所帮助。


推荐阅读