如何在 UICollectionViewCell 中添加 UICollectionView?

How do I add a UICollectionView inside a UICollectionViewCell?

我有一个使用可视化 XIB 文件创建的 UICollectionViewCell。

collection 视图水平显示。

我想在单元格的页脚中向该单元格添加另一个 UICollectionView。

单元格本身是一个"card"代表一些信息;

这张图有助于说明我所追求的;

我的想法是,我可以根据需要 show/hide 骰子图像,包括零;以水平方式作为当前 UICollectionCell 中的 child 部分。

尝试使用故事板;

到目前为止,我唯一的解决方案是手动创建 5 个图像视图,然后将它们附加到 collection 出口;然后我需要对每个模具图像进行 for-loop 到 show/hide。

  1. 在界面生成器中使用集合视图创建普通视图控制器
  2. 在单独的 XIB 中创建 CollectionViewCell。在此单元格内插入另一个 CollectionView
  3. 首先 VC 将 collectionViewCell 注册为 sectionHeader:

    tableView.register(UINib(nibName: DashboardPagerHeader.cellIdentifier, bundle: nil), forHeaderFooterViewReuseIdentifier: DashboardPagerHeader.cellIdentifier)
    
  4. 像以前一样在单独的 xib 中创建另一个 CollectionViewCell

  5. 在第 4 步中创建的 awakeFromNib 寄存器单元格的第一个集合视图单元格的 class 内部,如下所示:

    override func awakeFromNib() {
       super.awakeFromNib()
       collectionView.delegate = self
       collectionView.dataSource = self
       collectionView.register(UINib(nibName: "  DashboardHeaderCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: DashboardHeaderCollectionViewCell.cellIdentifier)
    }
    

您不需要 tableview,只需在您的 collectionview 单元格 xib 中拖动另一个集合并将其滚动 属性 设置为垂直,然后从内部 collectionview 创建 xib。

在外部 collectionview 单元格中使用该方法设置委托和数据源

// code
override func awakeFromNib() {
   super.awakeFromNib()
   serInnerCollectionView()
}

 func serInnerCollectionView() {

 collectionViewInner.delegate = self
   collectionViewInner.dataSource = self
   collectionViewInner.register(UINib(nibName: "  FooterCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "FooterCollectionViewCellIdentifier")

}