unten ist ein allgemeines Beispiel dafür, wie Sie Daten Sammlung Ansichten in Ihrer Tabellenansicht Zelle gelangen können. Auch dieses link ist ein Youtube-Tutorial zu diesem Thema.
Modelle:
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 Zelle:
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
}
}
Mutterzelle:
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
}
}
Child Zelle:
class CustomChildCell: UICollectionViewCell {
var child: Child?
}
Diese Repo https://github.com/ashfurrow/Collection-View-in-a-Table-View-Cell – kye
Gutes Bild helfen sollen - das ist möglich, aber komplex. Ich würde dringend empfehlen, ein neues 'Delegate/Data Source'-Modellobjekt für jede' UICollectionView 'zu erstellen und diese Objekte in einem' Array 'zu speichern, auf das dann in jeder' UITableView'-Zelle Bezug genommen wird. Sie müssen jede 'UICollectionView' mit dem richtigen Modellobjekt zur Laufzeit verbinden. Es ist machbar, aber es gibt viele bewegliche Teile, so dass Ihr Design klar durchdacht sein muss. –
@kye danke) Ich fragte bereits ashfurrow über mein Problem, aber es hat mir nicht geholfen: (( – Daryushka