まず、collectionViewセルファイルを作成する必要があります。ここでは、いくつかのコード例を示します。まずcollectionViewsのcontentOffsetの配列を作成します。
var contentOffsets = [Int : CGFloat]()
あなたがタグ-sが
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: booksListCellId) as! BooksListCell
cell.collectionView.tag = indexPath.row
return cell
}
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
let tableCell = cell as! BooksListCell
tableCell.collectionView.dataSource = self
tableCell.collectionView.delegate = self
tableCell.collectionView.reloadData()
if let offset = contentOffsets[tableCell.collectionView.tag] {
tableCell.collectionView.setContentOffset(CGPoint(x: offset, y: 0), animated: false)
}
}
func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {
let tableCell = cell as! BooksListCell
contentOffsets[tableCell.collectionView.tag] = tableCell.collectionView.contentOffset.x
}
と呼ばれるcollectionViewデリゲートメソッドを認識し、
のtableViewが含まれているあなたのcollectionViewデータソースとのViewControllerでデリゲートメソッドを実装するために、各行のcollectionViewsに設定することができますここにはtutorialがあります。
コレクションビューは通常のコレクションビューと同じようにテーブルセル内にあると考えることができます。カスタムコレクションビューセルを作成する必要があるかどうかは、あなたに依存します。私はコレクションビューのセルを作成することを好むでしょう。 –
@SaurabhPrajapatiあなたはそれに関連するリンクや説明を教えてください。私はiOSを初めて使用しています。 – Myanamwar
このリンクはあなたを助けることができます:https://ashfurrow.com/blog/putting-a-uicollectionview-in-a-uitableviewcell-in-swift/ – Pipiks