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")
}
}
}
}
}
}
Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Nov 14, 2017 at 22:47
\$\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\$ Commented 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)
}
answered Nov 15, 2017 at 3:24
\$\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 
}
answered Nov 24, 2017 at 11:00
\$\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.

answered Dec 9, 2017 at 11:24
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.