以编程方式在特定 UICollectionViewCell 内的 TableView? [英] TableView inside specific UICollectionViewCell programmatically?
问题描述
我正在尝试在 UICollectionViewCell 中添加一个 TableView,所以我想自己从 TableView 管理该单元格.所以更清楚地说,如果 indexPath.row 是 2,那么我想调用我的 tableView 单元格在 collectionview indexPath.row 中.
i am trying to add a TableView inside UICollectionViewCell, so i want to manage that cell by myself from TableView. So to be more clear, if indexPath.row is 2, then i want to call my tableView cell's inside collectionview indexPath.row.
请检查图片,我已经用红色做了我想做的事.我使用 UICollectionViewController 和 UICollectionViewControllerFlowLayout 以编程方式创建了所有内容.
Please check the picture, i have made with red colour what i want to do. I created everything programmatically, using UICollectionViewController and UICollectionViewControllerFlowLayout.
推荐答案
对于需要的人,我找到了解决方案:
For those who need it, i found the solution:
class CustomizedCell: UICollectionViewCell, UITableViewDataSource, UITableViewDelegate {
var tableView = UITableView()
let cellIdentifier: String = "tableCell"
override func layoutSubviews() {
super.layoutSubviews()
tableView.delegate = self
tableView.dataSource = self
tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellIdentifier)
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 4
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.value1, reuseIdentifier: cellIdentifier)
cell.textLabel?.text = "1 CUP"
cell.detailTextLabel?.text = "Whole"
return cell
}
}
然后在 CollectionView 的 viewDidLoad 方法中我这样做了:
Then at viewDidLoad method at CollectionView i did this:
collectionView?.register(CustomizedCell.self, forCellWithReuseIdentifier: "cell")
然后我在 UICollectionView 的 cellForRowAt indexPath 方法中这样调用:
And after that i have called like this at cellForRowAt indexPath method of UICollectionView:
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CustomizedCell
return cell
}
这篇关于以编程方式在特定 UICollectionViewCell 内的 TableView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!