1
\$\begingroup\$

I was looking for a way to write this code shorter and still get the same result. Can this be achieved?

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    if indexPath.row == 0 {
        print("connection status pressed")

        print("get")

        let connectionStatusSelectorViewController = ConnectionStatusSelector()
        //profileEditor.user = self.user
        navigationController?.pushViewController(connectionStatusSelectorViewController, animated: true)

    }else{
        if indexPath.row == 1 {
            print("account type pressed")


            let AccountSelectorViewController = AccountTypeSelector()

            navigationController?.pushViewController(AccountSelectorViewController, animated: true)


        }else{
            if indexPath.row == 2 {
                print("gender type pressed")

                let GenderSelectorViewController = GenderTypeSelector()
                navigationController?.pushViewController(GenderSelectorViewController, animated: true)

            }else{
                if indexPath.row == 3 {
                    print("service type pressed")

                    let serviceSelectorViewController = ServiceTypeSelector()
                    navigationController?.pushViewController(serviceSelectorViewController, animated: true)

                }else{
                    if indexPath.row == 4 {
                        print("distance pressed")

                    }else{
        print("nothing")
}
}
}
}
}
}
\$\endgroup\$
1
  • 1
    \$\begingroup\$ I proposed a different title that describes what the code does per site goals: "State what your code does in your title, not your main concerns about it.". Feel free to give it a different title if there is something more appropriate. \$\endgroup\$ Nov 14, 2017 at 22:56

3 Answers 3

1
\$\begingroup\$

One simple thing that would clean this up is to use the else if keyword. So you would do:

if indexPath.row == 0
{
    // code here
}
else if indexPath.row == 1
{
    // code for 1 here
}
else if indexPath.row == 2
{
    // code for 2 here
}
else if indexPath.row == 3
{
    ...etc.
}

This eliminates the nesting and the excessive number of closing curly braces at the end.

You could also remove the intermediate values. So one of the bodies of the "if" statement might look like this:

if indexPath.row == 2
{
    print("gender type pressed")
    navigationController?.pushViewController(GenderTypeSelector(), animated: true)
}
\$\endgroup\$
1
\$\begingroup\$

You can simplify a lot here.

enum Rowtype:Int {
case connection
case account
case gender
case service
case distance
}

struct ViewControllerFactory {
    static func viewController(rowType:Rowtype) -> UIViewController {
       switch rowType {
        case .connection:
        // return Connection View Controller
        // likewise handle other scenarios

        default:
           fatalError("Row type undefined")
        }
    }
}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    if let rowType = Rowtype.init(rawValue: indexPath.row) {
        return ViewControllerFactory.viewController(rowType)
    }
// handle other scenarios    
}
\$\endgroup\$
0
\$\begingroup\$

Expanding the answer by Abhishek, I would add a method to get the viewcontroller inside the enum.

enum Rowtype:Int {
    case connection
    case account
    case gender
    case service
    case distance

    func getViewController() -> UIViewController? {
        switch self {
        case .connection:
            return ConnectionStatusSelector()
        case .account:
            return AccountTypeSelector()
        case .gender:
            return GenderTypeSelector()
        case .service:
            return ServiceTypeSelector()
        default:
            return nil
        }
    }
 }

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    if let rowType = Rowtype.init(rawValue: indexPath.row),
        let destinationVC = rowType.getViewController() {
        navigationController?.pushViewController(destinationVC, animated: true)
    } else {
        // handle other scenarios
    }
}

This would allow you to write unit tests for the RowType enum.

\$\endgroup\$

Your Answer

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

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