2014-10-15 10 views
5

Mam custom cell class i custom nib, który zawiera projekt dla tej komórki. W moim storyboard nie widzę sposobu, aby połączyć tableview jako segue (jak masz z komórkami prototypowymi) Mam tam, ponieważ moja komórka jest dodana przez tableView:cellForRowAtIndexPath.UITableViewCell przejść do widoku szczegółów, gdy ładuję komórkę ze stalówki?

Czy istnieje sposób na połączenie tej sieci, aby móc nadal korzystać z storyboardu w celu połączenia komórki z kontrolerem widoku szczegółów?

Oto mój kod 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! 
} 

Odpowiedz

4

co się dowiedziałem co mogłem zrobić, to narzut ręczną segue (przeciągając od kontrolera) do kontrolera detali (jak pokazano segue z identyfikatorem: "Pokaż szczegóły"). Następnie mógłbym dodać następujący kod w moim widoku tabeli:

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

Który dałby mi funkcjonalność, którą chciałem.

+0

Jak można to zrobić z wielu komórek? – MazzaMan

+0

Nie wiem, co masz na myśli z wieloma komórkami, ale możesz użyć 'indexPath' w' didSelectRowAtIndexPath', aby zobaczyć, z której komórki pochodzi. – Bjarte

3

To jest to, co robię w 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 
    } 
} 
Powiązane problemy