Reputation: 195
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 to perform segue with identifier "goToDetail2" and so on. How can I achieve that?
Upvotes: 1
Views: 3915
Reputation: 72410
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
}
}
Upvotes: 1
Reputation: 1205
You need to override method didSelectCell:atIndexPath
. There you should check which cell was pressed(first second etc...). After that you need to have three segues in you controller (each segue should be from one controller to other controller, not from table view to controller). When you determine your next controller you can call method perfromSegueWithIdentifier
that will show needed controller.
func tableView(tableView: UITableView, didSelectCell cell: UITableViewCell onIndexPath indexPath: NSIndexPath) {
if let identifier: String? = [0 : "Identifier1", 1 : "identifier2", 2 : "identifier3"][indexpath.row] {
self.perforSegueWithIdentifier[identifier]
}
}
This code was writen fast without checking it's semantic correctness, so it may have some issues inside. But it should help you to understand what I'm talking about. As well I used Dictionary
intead of switch
or if else
because as for me it look better, you can use anything you want to define which cell was pressed. Hope it helps...
Upvotes: 0
Reputation: 507
If you use the delegate method tableView(_:didSelectRowAt:)
you will receive an indexPath. Once you have this, use it to get the cell you have tapped on by calling self.tableView.cellForRowAtIndexPath(indexPath)
. After that you can get the reuse identifier (cell.reuseIdentifer) and then perform an if/else to perform the correct segue.
EXAMPLE:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let cell = tableView.cellForRowAtIndexPath(indexPath)
if (cell.reuseIdentifer == "TableViewCell1")
{ //Do segue one
self.performSegueWithIdentifier("goToDetail", sender: post)
}
else if(cell.reuseIdentifer == "TableViewCell2")
{ //Do segue 2
self.performSegueWithIdentifier("goToDetail2", sender: post)
}
}
Upvotes: 0
Reputation: 797
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if indexPath.section == 0 {
let myWebView = self.storyboard!.instantiateViewControllerWithIdentifier(" Your ViewController") as! Your ViewController
self.presentViewController(myWebView, animated: true, completion: nil)
}
if indexPath.section == 1 {
let myWebView = self.storyboard!.instantiateViewControllerWithIdentifier(" Your ViewController") as! Your ViewController
self.presentViewController(myWebView, animated: true, completion: nil)
}
if indexPath.section == 2 {
let myWebView = self.storyboard!.instantiateViewControllerWithIdentifier("Your ViewController") as! Your ViewController
self.presentViewController(myWebView, animated: true, completion: nil)
}
}
Hope It Help. Thanks.
Upvotes: 2