0

I'm working with sections in a tableview in storyboard, where selecting a cell in the section will guide me to another screen. I have managed to do it but I feel that I use too much code and must see a better way to develop it, I have seen similar publications where they try to do the same, here I found this answer

Will the number of sections and items in the section change? If not, creating static cells and hooking up a segue to each cell to a different destination can be done with no code (all in Interface Builder).

is exactly what happens in my case and I would be interested to do of this form. I have fixed data that will not change. Now I try to develop this function in the best way possible and not as I currently have it. I must emphasize that I am a beginner in the development with swift.

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        switch indexPath.section {
        case 0:
            switch indexPath.row{
            case 0:
                self.performSegue(withIdentifier: "goToCreateAccountViewController", sender: nil)
                break
            case 1:
                break
            case 2:
                break
            default:
                break
            }
            break

        case 1:
            switch indexPath.row{
            case 0:
                break
            case 1:
                break
            case 2:
                break
            default:
                break
            }
            break

        case 2:
            break

        case 3:

            break

        default:
            break
        }
    }

cellForRow :

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell : UITableViewCell = UITableViewCell(style: UITableViewCell.CellStyle.subtitle, reuseIdentifier: "item_option")
        cell.textLabel?.text = options[indexPath.section][indexPath.row]
        cell.imageView?.image = UIImage(named: "icon-myprofile")
        cell.textLabel?.font = cell.textLabel?.font.withSize(14)
        return cell
    }
dbenitobaldeon
  • 324
  • 3
  • 20
  • Can you post the code behind cellForRow? – iDevid May 22 '19 at 21:41
  • Not a good way of doing it in my opinion. Even if you do it in storyboard it is going to be very difficult to maintain. Your data is not changing right now but it might in near future. I think it would be better for you if you learn a bit about Delegates and do it that way. – Galo Torres Sevilla May 22 '19 at 21:46
  • @iDevid update my question with the requested method – dbenitobaldeon May 22 '19 at 21:49

3 Answers3

0

You can shorten that giant switch to

switch (indexPath.section, indexPath.row) {
    case (0,0):
        self.performSegue(withIdentifier: "goToCreateAccountViewController", sender: nil)
    default:
        break
}
AgRizzo
  • 5,261
  • 1
  • 13
  • 28
0

I think you can do it using the storyboard like this:

enter image description here

Start by adding the View Controllers and the cells to the UITableView, and set the TableView content setting to Static Cells on the top right corner. After that just add the segues:

enter image description here

Claudio
  • 5,078
  • 1
  • 22
  • 33
0

Use a switch statement if you've multiple cases to handle.

But as evident from your code, you're only handling a single condition. In this case I don't see any need to use a switch statement.

You could have easily used a simple if statement to get that working.

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    if indexPath.section == 0 && indexPath.row == 0  {
        self.performSegue(withIdentifier: "goToCreateAccountViewController", sender: nil)
    }
}
PGDev
  • 23,751
  • 6
  • 34
  • 88