如何在UITableViewCell中嵌入UICollectionView并从UITableViewController填充数据
我来帮你搞定这个问题!其实核心就是让自定义的UITableViewCell自己管理内部的UICollectionView,然后在UITableViewController的tableView:cellForRowAt:方法里把对应的数据传递给cell就行。下面是具体的步骤和代码示例:
步骤1:创建包含UICollectionView的自定义UITableViewCell
首先我们需要一个自定义的TableViewCell,里面嵌入UICollectionView,并且让这个cell自己负责CollectionView的数据源和代理逻辑——这样TableViewController只需要专注给它传递数据,不用操心CollectionView的细节。
import UIKit class CollectionTableViewCell: UITableViewCell { // 用来存储CollectionView的数据源,赋值后自动刷新列表 var collectionData: [String] = [] { didSet { collectionView.reloadData() } } // 初始化CollectionView,设置布局和注册cell private lazy var collectionView: UICollectionView = { let layout = UICollectionViewFlowLayout() layout.itemSize = CGSize(width: 100, height: 100) layout.scrollDirection = .horizontal layout.minimumLineSpacing = 8 let cv = UICollectionView(frame: .zero, collectionViewLayout: layout) cv.translatesAutoresizingMaskIntoConstraints = false cv.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "CollectionCell") cv.dataSource = self cv.delegate = self cv.backgroundColor = .clear return cv }() override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) { super.init(style: style, reuseIdentifier: reuseIdentifier) setupCollectionViewLayout() } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } // 设置CollectionView的约束 private func setupCollectionViewLayout() { contentView.addSubview(collectionView) NSLayoutConstraint.activate([ collectionView.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 8), collectionView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 8), collectionView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -8), collectionView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -8), collectionView.heightAnchor.constraint(equalToConstant: 116) // 预留间距后的高度 ]) } } // 让自定义Cell遵守CollectionView的数据源和代理协议 extension CollectionTableViewCell: UICollectionViewDataSource, UICollectionViewDelegate { func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return collectionData.count } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionCell", for: indexPath) cell.backgroundColor = .systemBlue // 如果需要展示文字,可以在这里添加UILabel并设置文本 return cell } }
步骤2:在UITableViewController中配置并传递数据
接下来在你的UITableViewController里,注册这个自定义Cell,然后在tableView:cellForRowAt:方法中,把对应行的CollectionView数据赋值给Cell的collectionData属性——这一步就是你要的从TableViewController给CollectionView填充数据的关键!
class MyTableViewController: UITableViewController { // 模拟数据源:二维数组,每个子数组对应一行TableViewCell里的CollectionView数据 private let tableViewData: [[String]] = [ ["Apple", "Banana", "Cherry"], ["Dog", "Cat"], ["Red", "Green", "Blue", "Yellow"] ] override func viewDidLoad() { super.viewDidLoad() // 注册自定义TableViewCell tableView.register(CollectionTableViewCell.self, forCellReuseIdentifier: "CollectionTableViewCell") // 设置自动行高 tableView.rowHeight = UITableView.automaticDimension tableView.estimatedRowHeight = 132 } // MARK: - TableView数据源方法 override func numberOfSections(in tableView: UITableView) -> Int { return 1 } override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return tableViewData.count } override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { // 取出自定义Cell let cell = tableView.dequeueReusableCell(withIdentifier: "CollectionTableViewCell", for: indexPath) as! CollectionTableViewCell // 核心:把当前行对应的CollectionView数据传给Cell cell.collectionData = tableViewData[indexPath.row] return cell } }
额外技巧:处理CollectionView的点击事件
如果需要监听CollectionView单元格的点击,可以在自定义Cell里添加一个闭包,把点击事件回调给TableViewController,这样逻辑更清晰:
在CollectionTableViewCell里添加:
// 点击回调闭包,把选中的内容传出去 var didSelectItem: ((String) -> Void)? // 实现CollectionView的点击方法 func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { let selectedItem = collectionData[indexPath.item] didSelectItem?(selectedItem) }
然后在UITableViewController的tableView:cellForRowAt:方法里添加:
cell.didSelectItem = { selectedItem in print("选中了:\(selectedItem)") // 这里可以处理点击后的逻辑,比如跳转页面、弹出弹窗等 }
这样整个流程就顺畅了,每个组件各司其职,代码也更容易维护。
内容的提问来源于stack exchange,提问作者jhv
相关产品推荐
相关产品推荐

