からself.navigationController?.pushViewController(tableviewVC, animate: true)
を呼び出すことにより、ナビゲーションスタックにtableViewControllerをプッシュすることができますそのプロトコルをcollectionViewCell
に設定し、そのプロパティをcellForItemAt
メソッドに設定します。今すぐtableView
のdidSelectRowAt
メソッドの中で、segueのために渡したい細部を持つデリゲートメソッドを呼び出します。
protocol PassData {
func performSegue(with data: String) //Set argument type to Type that you want pass instead of String
}
今すぐあなたのViewController
とUICollectionViewCell
のデリゲートを設定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])
}
}
1語。伝説。 – Eli
@Ele Gladそれはあなたのために働く:) –