1
要素の水平スクロールのために、テーブルビューセル内にコレクションビューを作成したいと考えています。しかし、私は自分のコレクションビューを正しく満たす方法に問題があります。自分のデータにコレクションビューを接続する方法が必要です。コレクションコレクション内のテーブルビューセル
テーブルビューのセルを取得してループして、必要なコレクションビューを見つけ出し、セルにデータを戻すにはどうすればよいですか?以下
要素の水平スクロールのために、テーブルビューセル内にコレクションビューを作成したいと考えています。しかし、私は自分のコレクションビューを正しく満たす方法に問題があります。自分のデータにコレクションビューを接続する方法が必要です。コレクションコレクション内のテーブルビューセル
テーブルビューのセルを取得してループして、必要なコレクションビューを見つけ出し、セルにデータを戻すにはどうすればよいですか?以下
あなたは、テーブルビューセル内部のコレクションビューにデータを渡すことができる方法の一般的な例です。またこの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 {
}
テーブルビューセル:
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'に対して新しい' delegate/data source'モデルオブジェクトを作成し、 'UITableView'セルで参照されるオブジェクトを' Array'に格納することを強くお勧めします。それぞれの 'UICollectionView'を実行時に正しいモデルオブジェクトに接続する必要があります。それは可能ですが、動く部品がたくさんあるので、あなたのデザインは明確に考え出される必要があります。 –
@kye thanks)私はすでに私の問題についてashfurrowに尋ねましたが、それは助けになりませんでした。(( – Daryushka