2017-07-11 2 views
-1

私はコレクションビューをtableviewセルのビューの中に追加する必要があります。 以下の画像は正確に何が必要なのかを説明しています。すぐに3のテーブルビューのセル内にカスタムビューを持つcollectionViewを追加するには?

tableView 
- tableViewCell 
- contentView 
    -monthView 
    -detailedView 
     -collectionView 
      - collection Cell 
       -detailedImageView 

私はdetailedView(tableview cells second view)内にcollectionviewを追加しました。しかし、コレクションビューのcell.swiftファイルを作成する必要があるかどうか、そしてプロセス全体をどのように行うかは分かりません。

+0

コレクションビューは通常のコレクションビューと同じようにテーブルセル内にあると考えることができます。カスタムコレクションビューセルを作成する必要があるかどうかは、あなたに依存します。私はコレクションビューのセルを作成することを好むでしょう。 –

+0

@SaurabhPrajapatiあなたはそれに関連するリンクや説明を教えてください。私はiOSを初めて使用しています。 – Myanamwar

+0

このリンクはあなたを助けることができます:https://ashfurrow.com/blog/putting-a-uicollectionview-in-a-uitableviewcell-in-swift/ – Pipiks

答えて

0

まず、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があります。

関連する問題