Sie können erstellen für das und mit Ihrem Viewcontroller implementieren, wo Sie collectionView
haben, nachdem diese Instanz Eigenschaft machen von dieses Protokoll in Ihrem collectionViewCell
und setzen Sie diese Eigenschaft in cellForItemAt
Methode. Rufen Sie jetzt innerhalb der didSelectRowAt
-Methode von tableView
die Delegate-Methode mit dem Detail auf, das Sie für die Weiterleitung übergeben möchten.
protocol PassData {
func performSegue(with data: String) //Set argument type to Type that you want pass instead of String
}
Jetzt dieses PassData
Protokoll mit Ihrem ViewController
implementieren und innerhalb des cellForItemAt
stellen Sie den Delegierten von UICollectionViewCell
.
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)
}
}
Jetzt in Ihrer CustomCollectionViewCell
eine Instanz Eigenschaft machen passDelegate
vom Typ namens PassData?
danach in didSelectRowAt
Methode des Tableview ruft seine Delegatmethode.
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])
}
}
Ein Wort. Legende. – Eli
@Eli Froh, dass es für dich funktioniert :) –