2014-10-15 9 views
5

Ho uno custom cell class e uno custom nib che contiene il disegno per quella cella. Nel mio storyboard non vedo il modo di connettere lo tableview come un segue (come quello che hai con le celle del prototipo) che ho lì da quando la mia cella viene aggiunta tramite tableView:cellForRowAtIndexPath.UITableViewCell passa alla vista di dettaglio quando carico la cella dal pennino?

C'è un modo per ottenere questo seguito connesso in modo che possa continuare a utilizzare lo storyboard per connettere la cella al controller di visualizzazione dettagli?

Ecco il mio codice per tableView:cellForRowAtIndexPath:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    var cell = tableView.dequeueReusableCellWithIdentifier("cell") as MyTableViewCell? 

    if (cell == nil) { 
     tableView.registerNib(UINib(nibName: "MyTableViewCell", bundle: NSBundle(identifier: "com.company.InterfaceComponents")), forCellReuseIdentifier: "cell") 
     cell = tableView.dequeueReusableCellWithIdentifier("cell") as MyTableViewCell? 
    } 

    return cell! 
} 

risposta

4

Quello che ho scoperto che potevo fare, è quello di un aggiungere un segue manuale (trascinando da controllore) al controller dettagli (come esposizione segue con identificativo: "Mostra dettagli"). Quindi potrei aggiungere il seguente codice sulla mia vista tabella:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    self.performSegueWithIdentifier("showDetails", sender: tableView) 
} 

Quale mi avrebbe dato la funzionalità che volevo.

+0

Come vorresti fare questo con più celle? – MazzaMan

+0

Non sei sicuro di cosa intendi con più celle, ma puoi usare 'indexPath' in' didSelectRowAtIndexPath' per vedere da quale cella è selezionato. – Bjarte

3

Questo è quello che faccio a Swift 3:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    self.performSegue(withIdentifier: "show", sender: tableView) 
} 

override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
    if segue.identifier == "show" { 
     var indexPath = self.tableView.indexPathForSelectedRow 
     let selectedRow = indexPath?.row 
     let showVC = segue.destination as! NextViewController 
     //do some pre-setting for next VC here with "showVC", "selectedRow" and other var you set in next VC 
    } 
} 
Problemi correlati