Chad
Chad

Reputation: 163

Swift 3 Segue in UITableViewCell Controller

I configure dynamic cells in a table using a UITableViewCell controller and I want to perform a segue when a particular tap gesture is clicked

class PostCell: UITableViewCell {

    @IBOutlet var actionComments: UIImageView!

    var post: FeedPost!

    let postID: String = ""

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code

        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(likeTapped))
        tapGesture.numberOfTapsRequired = 1

        actionLike.addGestureRecognizer(tapGesture)
        actionLike.isUserInteractionEnabled = true

        let commentGesture = UITapGestureRecognizer(target: self, action: #selector(goToComments))
        commentGesture.numberOfTapsRequired = 1
        commentGesture.delegate = self

        actionComments.addGestureRecognizer(commentGesture)
        actionComments.isUserInteractionEnabled = true

    }

    func goToComments(sender: UITapGestureRecognizer) {



    }
}

this is my PostCell class (there is extra code i have just removed for sake of this post) and this is my tableview which is my newsfeedvc

class NewsFeedVC: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet weak var tableView: UITableView!

    var posts = [FeedPost]()

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let post = posts[indexPath.row]

        if let cell = tableView.dequeueReusableCell(withIdentifier: "PostCell") as? PostCell {

            cell.configureCell(post: post)

            return cell

        } else {

            return PostCell()

        }

    }

}

i have set the segue up with identifer but i cannot use the performsegue in the goToComments function in my postcell class?

Upvotes: 0

Views: 409

Answers (1)

Nirav D
Nirav D

Reputation: 72410

To solve your problem you have two options.

  1. Create one delegate/protocol and implement it with NewsFeedVC and create instance of in inside the PostCell. After that set that delegate in cellForRowAt method now in goToComments use that delegate and call its method that you have implement in NewsFeedVC and inside that method perform the segue.

  2. Instead of adding gesture in awakeFromNib method add it in the cellForRowAt method show you have its action method inside your NewsFeedVC. Now you can easily perform your segue.

Upvotes: 1

Related Questions