2017-05-29 6 views
0

私はコレクションビューのセルに埋め込まれているUITableViewCellからUITableViewControllerをプッシュしようとしています。UITableViewCellのUICollectionViewからTableViewControllerをプッシュ

したがって、構造はUICollectionView> UICollectionViewCell> UITableView> UITableViewCellです。

TableViewCellがクリックされたときにセグを呼びたいですか?

識別子にsegueを実行する機能にアクセスできない場合はどうすればよいですか?

答えて

2

からself.navigationController?.pushViewController(tableviewVC, animate: true)を呼び出すことにより、ナビゲーションスタックにtableViewControllerをプッシュすることができますそのプロトコルをcollectionViewCellに設定し、そのプロパティをcellForItemAtメソッドに設定します。今すぐtableViewdidSelectRowAtメソッドの中で、segueのために渡したい細部を持つデリゲートメソッドを呼び出します。

protocol PassData { 
    func performSegue(with data: String) //Set argument type to Type that you want pass instead of String   
} 

今すぐあなたのViewControllerUICollectionViewCellのデリゲートを設定cellForItemAt内部でこのPassDataプロトコルを実装します。その後タイプPassData?passDelegateをという名前のCustomCollectionViewCellする1つのインスタンスプロパティで今すぐ

class ViewController: UIViewController, PassData, UICollectionViewDelegate, UICollectionViewDataSource { 
    //Other methods   

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CustomCollectionViewCell 
     cell.passDelegate = self 
     return cell 
    } 

    //Add the `performSegue(with:)` delegate method of PassData 
    func performSegue(with data: String) { 
     //Perform segue here 
     self.performSegue(withIdentifier: "SegueIdentifier", sender: data) 
    } 
} 

のtableViewのdidSelectRowAt方法でそのデリゲートメソッドを呼び出します。

class CustomCollectionViewCell: UICollectionViewCell, UITableViewDelegate, UITableViewDataSource { 

    var passDelegate: PassData? 

    //Your other methods 

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
     //Pass whatever data that you want to pass 
     self.passDelegate?.performSegue(with: array[indexPath.row]) 
    }   
} 
+0

1語。伝説。 – Eli

+0

@Ele Gladそれはあなたのために働く:) –

0

あなたはそれがのインスタンスプロパティを作成した後、あなたは、そのための1 を作成し、collectionViewを持っているところあなたのViewControllerでそれを実装することができ、テーブルビューのデリゲートメソッドdidSelectRowAtIndexPath

関連する問題