2016-09-07 6 views
1

私はTableViewを持っていますTableViewCell私はUICollectionViewを追加しました。ユーザーがUICollectionViewをタップすると、次のデータをviewControllerに渡したいのですが、残念ながらdidselectアイテムがある場所でUITableViewCellクラス内でsegueを実行することはできません。ここでセグを実行する方法はありますか?PerfomSegueのUITableViewCellクラス

コード全体

class UserLibraryViewController: UIViewController { 

extension UserLibraryViewController : UITableViewDelegate { } 

extension UserLibraryViewController : UITableViewDataSource { 

func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 
    return myBooksGenre[section] 
} 

func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    return myBooksGenre.count 
} 

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return 1 
} 

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("userBooksTableViewCell") as! UserLibraryTableViewCell 
    cell.tableIndexPath = indexPath 

    return cell 
} 
} 

TableViewCell

class UserLibraryTableViewCell: UITableViewCell { 
@IBOutlet weak var collectionView: UICollectionView! 
var tableIndexPath: NSIndexPath? 

} 

extension UserLibraryTableViewCell : UICollectionViewDataSource { 

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

    return 12 
} 

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("userBooksCollectionViewCell", forIndexPath: indexPath) as! UserLibraryCollectionViewCell 



    return cell 
} 

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

    print("\(self.tableIndexPath!.section) ---- \(indexPath.item) \(tableSectionHeader[self.tableIndexPath!.section]) ") 


} 
} 

extension UserLibraryTableViewCell : UICollectionViewDelegateFlowLayout { 

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { 
    let itemsPerRow:CGFloat = 4 
    let hardCodedPadding:CGFloat = 5 
    let itemWidth = (collectionView.bounds.width/itemsPerRow) - hardCodedPadding 
    let itemHeight = collectionView.bounds.height - (2 * hardCodedPadding) 
    return CGSize(width: itemWidth, height: itemHeight) 
} 

} 

答えて

2

すなわちTableViewCell内部そのインスタンスを作成してのdidSelectItemAtIndexPathにおけるその後、UserLibraryViewControllerでプロトコルを実装した後に、あるプロトコルを作成しますCollectionViewこのようなメソッドを呼び出すデリゲートインスタンスを使用します。

protocol CustomCollectionDelegate { 
    func selectedCollectionCell(indexPath: NSIndexPath) 
} 

class UserLibraryTableViewCell: UITableViewCell { 
    @IBOutlet weak var collectionView: UICollectionView! 
    var tableIndexPath: NSIndexPath? 
    var delegate: CustomCollectionDelegate? 
} 

CollectionViewのごdidSelectItemAtIndexPath内のこのメソッドを呼び出します。

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

    print("\(self.tableIndexPath!.section) ---- \(indexPath.item) \(tableSectionHeader[self.tableIndexPath!.section]) ") 
    self.delegate?.selectedCollectionCell(indexPath) 
} 

今、このようUserLibraryViewControllerで、このプロトコルを実装し、cellForRowAtIndexPathにデリゲートを設定します。

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("userBooksTableViewCell") as! UserLibraryTableViewCell 
    cell.tableIndexPath = indexPath 
    cell.delegate = self 
    return cell 
} 

extension UserLibraryViewController : CustomCollectionDelegate { 
    func selectedCollectionCell(indexPath: NSIndexPath) { 
     self.performSegueWithIdentifier("Identifier", sender: indexPath) 
    } 
} 

注:私はあなたのカスタムオブジェクトまたはDictionary/Arrayでそれを変更することができ、パラメータとしてNSIndexPathとプロトコルメソッドを追加しました。

+0

動作しません。拡張機能は呼び出されません。ブレークポイントを入れようとしました。 – Nitesh

+0

申し訳ありませんが、デリゲートを自分で設定するのを忘れました。編集した 'cellForRowAtIndexPath'の答えを確認してください。 –

+0

ありがとう。私の要求に従って今働いています。 :) – Nitesh

関連する問題