2013-05-20 10 views
7

私はカスタムセルを持つテーブルビューを持っています。これは配列から内容を取得しますが、これはすべてうまくいきます。このコンテンツに、さまざまな数のサムネイル画像のコレクションビューを各カスタムテーブルビューセルに追加したいと思います。カスタムテーブルビューのセルにコレクションビューを埋め込む

私はストーリーボードのカスタムセルにコレクションを追加しました。 私はカスタムCell.mにコレクションをリンクしました。 コレクションカスタムセルを作成し、画像をリンクしました。

ここからですコレクションビューを作成するメソッドをカスタムテーブルCell.mに配置するか、ビューコントローラーに配置するかどうかはわかりません.m?私はストーリーボードからのイメージを持って、あなたはカスタムテーブルのセルを見ることができますし、下部にはコレクションビュー(水平スクロール)があります。私はまた、情報が欠落している場合は、私はごめんなさい、情報が助けるかもしれないことがわかりません。

Picture of Custom Table Cell

+0

私はこれを見つけましたが、それはテーブル内の基本的なテーブルです。 http://iosstuff.wordpress.com/2011/06/29/adding-a-uitableview-viewable-a-uitableviewcell/ Table View内のCollection View(カスタムCellを使用)に変換するのは面倒ですが、これは役に立ちました。間違ったキーワードを探しているのですか? –

+0

[このチュートリアル](http://ashfurrow.com/blog/putting-a-uicollectionview-in-a-uitableviewcell)はあなたが探しているものです。 –

答えて

4

カスタムUITableViewCellクラス内UICollectionViewDelagateDataSourceを置く必要があります。

ここにはnice tutorialがあります。

これはtableviewセル内のtableviewについてですが、その考え方はかなり似ています。

Good Luck!

+0

多くのありがとう、私は見てみましょう。 –

+0

それはよさそうだ。私はスクロールビューを追加して、その中にサブビューを配置し、各ビューにジェスチャーを追加しました。これは動作しますが、私はあなたの方法がより速いかもしれないと思います...私は試して実装します、ありがとう。 –

2

ここには迅速な解決策があります。 コレクションビューのデータソースをセットアップし、tableview委譲メソッドcellForRowAtIndexPathを使用してtableviewセルにデリゲートする必要があります。この後、collectionviewデータソースを使用し、tableveiewデータソースとデリゲートを確認したviewControllerにデリゲートメソッドをデリゲートします。

extension MainViewController:UITableViewDataSource, UITableViewDelegate { 

// .....do some table view setup like numberOfRowsInSection ...... 

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("yourReusecellIdentifier", forIndexPath: indexPath) as! yourCustomCell 

cell.setCollectionViewDataSourceDelegate(self, forRow: indexPath.row) 

return cell 
} 
} 

ここcell.setcollectionViewDataSourceDelegate(自己、forRow:indexPath.rom)ここmainViewControllerためのコードであるコードは、このドラッグ後のテーブルビューセルにテーブルビューのセルにcollectionview出口をcollectionviewデリゲートとデータソースを設定します。 tableViewセルに次のようにcollectionViewデリゲートを設定するmehod setcollectionViewDataSourceDelegateを追加します。

class yourCustomCell: UITableViewCell { 

//MARK:- Properties 

@IBOutlet weak var collectionView: UICollectionView!  

//MARK:- initialization methods 

override func awakeFromNib() { 
    super.awakeFromNib() 
    setupView() 
} 

override func setSelected(selected: Bool, animated: Bool) { 
    super.setSelected(selected, animated: animated) 

} 

//MARK:- Setup collectionView datasource and delegate 

func setCollectionViewDataSourceDelegate 
    <D: protocol<UICollectionViewDataSource, UICollectionViewDelegate>> 
    (dataSourceDelegate: D, forRow row: Int) { 

    collectionView.delegate = dataSourceDelegate 
    collectionView.dataSource = dataSourceDelegate 
    collectionView.tag = row 
    collectionView.reloadData() 
} 
} 

このあなたにコレクションビューのデリゲートメソッドを使用しますが、コントローラ表示した後:明確な説明訪問このhttps://ashfurrow.com/blog/putting-a-uicollectionview-in-a-uitableviewcell-in-swift/

について

extension MainViewController: UICollectionViewDelegate, UICollectionViewDataSource { 

func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int { 

     return 2 

} 

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 

     return 5 
} 

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 

     let cell = collectionView.dequeueReusableCellWithReuseIdentifier("collectionViewReuseIdentifier", forIndexPath: indexPath) as! YourCollectionViewCustomCell 

     .......cell configure.... 

     return cell 

} 
} 

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { 

print("get selected collectionview itemindex \(indexPath.row)") 

} 
} 

関連する問題