【问题标题】:Data Source for CollectionView inside CollectionViewCell is always nilCollectionViewCell 中 CollectionView 的数据源始终为 nil
【发布时间】:2020-02-24 00:31:50
【问题描述】:

我在UICollectionViewCell 中有一个UICollectionView,还有一个单独的NSObject,即dataSource。我可以为外部UICollectionView 设置dataSource,但不能为内部设置。

这是包含内部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")

    }

}

还有包含外部UICollectionViewUIView

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

这两个方法都被调用了,但dataSourcedelegates 从未被设置。我也完全遵循this 教程(甚至使用UITableView),但它仍然不会设置dataSourcedelegate。我究竟做错了什么?

【问题讨论】:

    标签: ios swift xcode uitableview uicollectionview


    【解决方案1】:

    我认为这会有所帮助

    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 link 的教程,将您的代码与该教程进行比较,看看您错过了什么。

    希望这会有所帮助。

    【讨论】:

      【解决方案2】:

      解决了。问题在于:

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

      需要改成这样:

      func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
      print("WillDisplay")
      guard let featuredCell = cell as? FeaturedCell else { return }
      featuredCell.setCollectionViewDataSourceDelegate(featuredData, forRow: indexPath.item)
      

      }

      【讨论】:

        猜你喜欢
        • 2021-05-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多