2017-05-29 2 views
0

Also ich versuche, einen UITableViewController von einer UITableViewCell, die in einer Sammlung View-Zelle eingebettet ist, zu schieben.Push TableViewController von einem UICollectionView mit UITableViewCells

So ist die Struktur UICollectionView> UICollectionViewCell> UITableView> UITableViewCell.

Soll ich ein Segment aufrufen, wenn auf die TableViewCell geklickt wird?

Wie kann ich dies tun, da Sie nicht auf die Funktion zum Ausführen von Überleitung für Bezeichner zugreifen können?

Antwort

2

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]) 
    }   
} 
+0

Ein Wort. Legende. – Eli

+0

@Eli Froh, dass es für dich funktioniert :) –

0

Sie die Tableviewcontroller zu Navigationsstapel schieben kann durch self.navigationController?.pushViewController(tableviewVC, animate: true) aus Tabellensicht Delegatmethode Aufruf didSelectRowAtIndexPath

Verwandte Themen