Leo Guo Leo Guo - 3 months ago 35
Swift Question

Swift: How to perform Segue for different UITableViewCell to different ViewController?

I have a

HomePageTableViewController
, which contains three customized
TableViewCell
, each
TableViewCell
contains a
UICollectionView
. I want to perform segue when user tapped on each
TableViewCell
and go to different
ViewController
to show the detail of the
CollectionView
. I used this code:

import UIKit

class HomePageTableViewController: UITableViewController, PostDelegate {


override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 3
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {


if indexPath.section == 0 {

let cell = tableView.dequeueReusableCellWithIdentifier("TableViewCell1") as! HomePageTableViewCell1

cell.getCategory()
// cell.scrollToNextCell()
// cell.startTimer()
cell.postDelegate = self
tableView.rowHeight = 260
return cell
}

else if indexPath.section == 1 {

let cell = tableView.dequeueReusableCellWithIdentifier("TableViewCell2") as! HomePageTableViewCell2

cell.getCategory()
tableView.rowHeight = 140


return cell
}
else {

let cell = tableView.dequeueReusableCellWithIdentifier("TableViewCell3") as! HomePageTableViewCell3

cell.getCategory()
tableView.rowHeight = 470
return cell
}

}


func selectedPost(post: Posts) {
//You will get your post object here, do what you want now

self.performSegueWithIdentifier("goToDetail", sender: post)


}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {


let post = sender as! Posts

let postDetailPage = segue.destinationViewController as? DetailTableViewController
postDetailPage!.passPost = post


}


this code does the job whatever which
TableViewCell
I tapped, it goes to the same segue with identifier "goToDetail". for example, for the
HomeTableTableViewCell2
, I want it perform segue with identifier "goToDetail2", how can I achieve that?

Answer

From looking your code looks like you have created one protocol and created the instance inside all 3 CustomTableView cell. After that you are calling that delegate method on selection of your collectionView.

To solved your problem just create one more parameter of type Int with your protocol method and pass the CustomTableView index with it.

So now your protocol PostDelegate will be looks like this.

protocol PostDelegate {
    func selectedPost(post: Posts, index: Int) 
    //Others method if you have
}

Now call these method from your different cell like this way

In HomePageTableViewCell1

self.postDelegate.selectedPost(post: post, index: 0)

In HomePageTableViewCell2

self.postDelegate.selectedPost(post: post, index: 1)

In HomePageTableViewCell3

self.postDelegate.selectedPost(post: post, index: 2)

Now change the implementation of selectedPost method inside HomePageTableViewController like this

func selectedPost(post: Posts, index: Int) {

    if (index == 0) {
        self.performSegueWithIdentifier("goToDetail", sender: post)
    }
    else if (index == 1) {
        //Perform segue with Second ViewController
    }
    else {
        //Perform segue with Third ViewController
    } 
}

Edit: To know which segue you have perform inside prepareForSegue you can use segue.identifier for that.

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if (segue.identifier == "goToDetail") { 
        let post = sender as! Posts
        let postDetailPage = segue.destinationViewController as? DetailTableViewController
        postDetailPage!.passPost = post
    }
    else if (segue.identifier == "secondViewSegue") { 
        let post = sender as! Posts
        let postDetailPage = segue.destinationViewController as? SecondViewController
        postDetailPage!.passPost = post
    }
    else { 
        let post = sender as! Posts
        let postDetailPage = segue.destinationViewController as? ThirdViewController
        postDetailPage!.passPost = post
    }
}
Comments