1

If a label is tapped in a table view cell, I would like to perform a Segue to take you to another page.

I get the following error Value of type [UITableViewCell] has no member 'performSegueWithIdentifier'

class IdeaCell: UITableViewCell {

    override func awakeFromNib() {
        super.awakeFromNib()

        let tapUser = UITapGestureRecognizer(target: self, action: #selector(IdeaCell.goToUserPage(_:)))
        tapUser.numberOfTapsRequired = 1
        usernameLbl.addGestureRecognizer(tapUser)
    }

func goToUserPage(sender: UITapGestureRecognizer) {
        self.performSegueWithIdentifier("goToTableFromOriginal", sender: nil)
    }
}
Iyad Atuan
  • 33
  • 3
  • 2
    A cell isn't a UIViewController so it can't perform a segue. You should use a protocol to have the cell invoke a function on the view controller vis delegation and the view controller function can perform the segue – Paulw11 Sep 25 '16 at 21:55
  • http://stackoverflow.com/a/22761617/4475605 – Adrian Sep 25 '16 at 22:23

1 Answers1

0

Rather than trying to add a tap gesture recognizer to your cells, it would be simpler and cleaner if you implemented the table(_:didSelectRowAt:) method (Swift 3 version of the method signature) in the view controller that manages the table view. (Possibly a table view controller?)

Failing that, you'll need to create a protocol called something like TableViewCellDelegate that includes a didSelect(cell:UITableViewCell) method (or something along those lines). You'd then add a delegate property to your custom table view cells and set that property to the view controller in the cellForRow(at:) method.

Then in the cell, you'd invoke your didSelect(cell:UITableViewCell) on the cell's delegate.

In your view controller you'd use indexPath(for cell: UITableViewCell) to figure out which cell was tapped and use that to decide what action to take on the tapped cell (invoking a segue, in your case.)

Duncan C
  • 128,072
  • 22
  • 173
  • 272