【问题标题】:UIcollectionview inside UIcollectionviewcell and how to display dataUIcollectionviewcell 内的 UIcollectionview 以及如何显示数据
【发布时间】:2018-05-07 07:52:25
【问题描述】:

我需要创建一个collectionView A 包含几种类型的单元格。其中一个单元格包含可以水平滚动的collectionView B。所以collectionView A 会垂直滚动。

我可以知道如何创建它以及如何将数据显示到collectionView B 中吗?

再次感谢大家

【问题讨论】:

标签: ios swift uicollectionview uicollectionviewlayout


【解决方案1】:

Collection A have Cells 让我们将目标单元格称为ASubCell

然后在 ASubCell 我们将拥有 Collection B 及其所有委托,并且您将创建委托说 ASubCellDelegate 以将 Collection B 上的操作发送到 Collection A 的 ViewController,这里是示例

import UIKit

protocol ASubCellDelegate {
    // CollectionView Cell Pressed
    func onSubCellAtcolectionBPressed(_item : YourData)
}
class ASubCell: UICollectionViewCell {

    @IBOutlet weak var collectionViewB : UICollectionView!
     var delegate : ASubCellDelegate?

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        // setup your Collection View B
    }

}


// set up your horizontal Collection View
extension UICollectionViewCell:UICollectionViewDelegate,UICollectionViewDataSource {
    public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 10
    }
    public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
       let cell  =  collectionView.dequeueReusableCell(withReuseIdentifier: "YourBCellID", for: indexPath) as! YourBCellID
        return cell
    }
    public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

        if delegate != nil {
            self.onSubCellAtcolectionBPressed(YourData)
        }
    }

}

【讨论】:

  • 兄弟,我需要在主视图控制器上加载所有数据,然后将数据显示到集合视图 A 和 B,而不是通过单击单元格。
  • 是的,你会将集合的 dataSource 传递给集合 A 的单元格,那会做什么
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多