表格视图单元格内的集合视图
问题描述:
我想在表格视图单元格内创建集合视图以便水平滚动元素。但我有问题如何正确填写我的收藏意见。我需要一种方法将集合视图与我的数据连接起来。表格视图单元格内的集合视图
如何获取并循环查看表格视图单元格以查找所需的集合视图并返回包含数据的单元格?下面
答
是的,你可以怎样度过的表视图单元格中的数据收集意见的一般示例。此link是关于此主题的YouTube教程。
型号:
class ListOfParents: NSObject {
var parents:[Parent]?
}
class Parent: NSObject {
var children: [Child]?
static func fetchParents(_ completionHandler: @escaping (ListOfParents) ->()) {
//fetch parents data
}
}
class Child: NSObject {
}
的TableView细胞:
class CustomTableViewController: UITableViewController {
var listOfParents: ListOfParents?
override func viewDidLoad() {
super.viewDidLoad()
Parent.fetchparents { (listOfParents) in
self.listOfParents = listOfParents
}
tableView.register(CustomParentCell.self, forCellReuseIdentifier: "tableCell")
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
guard let parentsCount = listOfParents?.parents?.count else {return 0}
return parentsCount
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "tableCell", for: indexPath) as! CustomParentCell
cell.parent = listOfParents?.parents?[indexPath.item]
return cell
}
}
亲代细胞:
class CustomParentCell: UITableViewCell, UICollectionViewDelegate, UICollectionViewDataSource {
var parent: Parent? {
didSet {
// set child value here
}
}
lazy var collectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
return collectionView
}()
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
collectionView.delegate = self
collectionView.dataSource = self
collectionView.register(CustomChildCell.self, forCellWithReuseIdentifier: "childCellID")
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
guard let childrenCount = parent?.children?.count else {return 0}
return childrenCount
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "childCellID", for: indexPath) as! CustomChildCell
cell.child = parent?.children?[indexPath.item]
return cell
}
}
子细胞:
class CustomChildCell: UICollectionViewCell {
var child: Child?
}
这应该回购有助于https://github.com/ashfurrow/Collection-View-in-a-Table-View-Cell – kye
良好的形象 - 这是可能的,但复杂。我强烈建议为每个'UICollectionView'创建一个新的'委托/数据源'模型对象,并将这些对象存储在一个'Array'中,然后在每个'UITableView'单元格中引用它们。您需要在运行时将每个“UICollectionView”连接到正确的模型对象。这是可行的,但有很多移动部件,所以你的设计需要清楚地思考。 –
@kye谢谢)我已经问过ashfurrow关于我的问题,但它并没有帮助我:(( – Daryushka