Reputation: 3335
I am having an issue with designing a storyboard. I currently have an app in the app store that was designed with XIB files. I have a tableView placed on a UIViewController - not a tableViewController - that loads a unique viewController for each row that is selected.
I am trying to convert this concept to a storyboard. I've done the following
Place a UIViewController in the Storyboard.
Place a UITableView on the UIViewController so I can customize the table view.
I tried making connections to multiple ViewControllers from the UITableViewCell and I am not allowed.
I tried creating multiple segues from the UIViewController - which I can - but when I click on the cells the segues are not fired. I tried using didSelectRowAtIndexPath: and prepareForSegue:
Since this did not work I tried creating a project with a UITableViewController and then created multiple segues from the UITableViewController. I then named each segue and used the didSelectRowAtIndexPath: method to test the selected cell and called performSegueWithIdentifier: and this did not work. As I clicked on cells I would get random loading of the incorrect viewController. I've copied a bit of my code and few screen shots below.
I am wondering if I am blatantly missing something obvious or do I have to revert to a xib format for this type of project?
override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath)
{
if indexPath.row == 0
{
println("Segue1")
self.performSegueWithIdentifier("Segue1", sender: self)
}
else if indexPath.row == 1
{
println("Segue2")
self.performSegueWithIdentifier("Segue2", sender: self)
}
else if indexPath.row == 2
{
println("Segue3")
self.performSegueWithIdentifier("Segue3", sender: self)
}
}
Take care,
Jon
Upvotes: 5
Views: 10100
Reputation: 213
Swift 3 Code
Well, look at my piece of code I use to perform dynamic segue based on the type of the device type in the cell. :) Hope it helps.
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let device = Cache.DEVICELIST[indexPath.row]
let deviceType = device.type
switch deviceType {
case "camera":
self.performSegue(withIdentifier: "DashCellToCamera", sender: self)
case "gateway" :
self.performSegue(withIdentifier: "DashCellToGateway", sender: self)
case "tracker" :
self.performSegue(withIdentifier: "DashCellToTracker", sender: self)
case "panicbutton" :
self.performSegue(withIdentifier: "DashCellToPanicButton", sender: self)
default:
break
//do nothing just ignore this
}
}
Upvotes: 1
Reputation: 149
I used a combination of the comment by Allen. Initially I wasn't able to create the segues I needed. I created a navigation controller that uses my own view controller that holds a tableview. Then I created the associated cells for the prototype cells and switched them to a static cell. I only have a limited amount, 3 at the time. This allowed me to created the segues to the appropriate views in the storyboard. Then I changed the cells back to prototype cells and it maintained the connection to the segues. All that is left was creating the associated switch statement in the table view to go to the correct segue. Just make sure you name your segues with the identifiers listed in your code.
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath:NSIndexPath) {
//Add cases here to move to different segues
switch indexPath.row{
case 0: self.performSegueWithIdentifier("courses", sender: self);
break;
case 1: self.performSegueWithIdentifier("finals", sender: self);
break;
case 2: self.performSegueWithIdentifier("academic", sender: self);
break;
default:
break
}
}
Upvotes: 2
Reputation: 415
The method
override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath)
is called when an item is deselected.
To call a method when an item is selected, change the method name to override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
Upvotes: 6