CollectionViewCell内CollectionView的数据源始终为零

问题描述 投票:0回答:1

我在CollectionViewCell中有一个CollectionView,还有一个单独的NSObject,它是数据源。我可以为外部collectionView设置数据源,但不能为内部setView设置数据源。

这里是包含内部collectionView的单元格:

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")

}

}

以及包含外部collectionView的UIView:

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)
}

}

这两种方法都被调用,但是从未设置数据源和委托。我也完全按照this教程进行操作(即使使用tableView,也是如此),它仍然不会设置数据源或委托。我究竟做错了什么?

ios swift xcode uicollectionview uicollectionviewcell
1个回答
0
投票

我认为这会有所帮助

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")
    }
}

和在MainView中

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 link进行操作,将您的代码与此进行比较,看看您错过了哪里。

希望这会有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.