Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableViewCell segue to detail view when I load cell from nib?

I have a custom cell class and a custom nib that contains the design for that cell. In my storyboard I don't see the way to connect the tableview as a segue (Like you have with prototype cells) I have there since my cell is added via tableView:cellForRowAtIndexPath.

Is there a way of getting this segue connected so I can continue to use the storyboard to connect cell to detail view controller?

Here is my code for 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!
}
like image 888
Bjarte Avatar asked Oct 15 '14 08:10

Bjarte


2 Answers

What I found out I could do, is to a add a manual segue (by dragging from controller) to the details controller (as Show segue with identifier: "showDetails"). Then I could add the following code on my table view:

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

Which would give me the functionality I wanted.

like image 67
Bjarte Avatar answered Sep 28 '22 08:09

Bjarte


This is what I do in 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
    }
}
like image 33
Edwin_zl Avatar answered Sep 28 '22 10:09

Edwin_zl