首页 > 解决方案 > 每个 UICollectionView 单元格中的 UITableView

问题描述

我一直在寻找有关在 UICollectionView 单元格中获取 UITableView 的信息,但到目前为止,我只找到了确切的对立面:TableView 单元格中的 CollectionViews。我的方法如下:

集合视图控制器:

class CollectionViewController: UICollectionViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 5
    }

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionViewCell", for: indexPath) as! CollectionViewCell

        return cell

    }
}

自定义 CollectionViewCell:

class CollectionViewCell: UICollectionViewCell {

    override func awakeFromNib() {
        super.awakeFromNib()
        let tableView = UITableView()
        self.contentView.addSubview(tableView)
    }

}

目标是让信息显示在水平滚动的页面中,并且每个页面可以有很多行(因此 CollectionViews 中的 TableViews)。但是,我似乎无法弄清楚如何为每个添加到 CollectionView 单元格的 TableView 实例添加一个 TableViewController。

提前致谢。

标签: swiftuitableviewscrolluicollectionviewcell

解决方案


不确定这是否真的值得回答......但回到我们的评论讨论......

您可以像任何 UIView 或 UIViewController 一样使 UICollectionView 符合 tableview 协议。


推荐阅读