2

I've an UITableView which loads data from a database file. It has different sections. I have managed to design a custom UITableViewCell programmatically. When I touch a cell, I'm segueing to an UIViewController. The segue is added from the Interface Builder. Now, I need to load different contents (i.e. UILabel,UIButton) in the new UIViewController based on the section number of the UITableView. For more visualization, I'm adding images below.

How to accomplish this? Should I use custom UIViewController subclass for each action? Or is there any easier way to do this?

1st view

view 1

2nd view

view 2

3rd view

view 3

3
  • Do you mean you're segueing to a UIViewController? Aug 3, 2016 at 15:27
  • Yes, I'm segueing to an UIViewController @NiñoScript
    – nayem
    Aug 3, 2016 at 15:38
  • I edited your question to reflect that. ;) Aug 3, 2016 at 15:54

1 Answer 1

2

In code, you can use the prepareForSegue method, check the identifier, and pass the next UIViewController the relevant data.

If the interface is different for each kind cell, then you should use different UIViewControllers for each kind of interface. All of this can be done through the storyboard, by "ctrl-dragging" from each prototype cell to a different controller.


If you don't want to use different prototype cells for different data. (Which would make this very easy to do with the Storyboard), then instead of segueing through the storyboard, call the segue via code in the didSelectRowAtIndexPath method of your UITableView delegate. Like this:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    switch indexPath.section {
    case 0:
        performSegueWithIdentifier("some_segue_id", sender: self)
    case 1:
        performSegueWithIdentifier("another_segue_id", sender: self)
    default:
        print("not a known section")
    }
}
4
  • But the problem is, I'm using just one reusable cell. Contents are different as I'm fetching them from a database. And I want to segue to an UIView or UIViewController depending on the section number of the selected cell
    – nayem
    Aug 3, 2016 at 15:42
  • You could easily use more reusable cells, that would also make it easier to change the interface of each kind of data if you ever want to. But if you don't want to, I added a code example to my answer above. Aug 3, 2016 at 15:49
  • Okay. But how to add more segues? I've added one segue from the UITableViewCell' to UIViewController` by control-dragging in the storyboard and given it an identifier. But I'm not able to add more segues from one cell to different UIViewController scene.
    – nayem
    Aug 3, 2016 at 16:01
  • 1
    Delete that segue, and add many segues from the UITableViewController to different UIViewControllers. And then perform the segue with the code I gave you instead of with the storyboard (that's what I meant with "instead of segueing through the storyboard") Aug 3, 2016 at 16:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.