Brian Marsh
Brian Marsh

Reputation: 617

didSelectRowAtIndexpath nested if statements

What's the best way of changing the action of didSelectRowAtIndexPath based upon the indexPath.row.

For example, 5 rows leads me to have something like this:

switch indexPath.row {
 case 0:
  if offlineMode {
    // do this
  } else {
    // do something else
  }
 case 1:
  if offlineMode {
    // do this
  } else {
    // do something else
  }
 case 2:
  if offlineMode {
    // do this
  } else {
    // do something else
  }
 case 3:
  if offlineMode {
    // do this
  } else {
    // do something else
  }
 case 4:
  if offlineMode {
    // do this
  } else {
    // do something else
  }
 default:
  // default behaviour
}

So, I can separate this code into doActionOnSelect(indexPath: NSIndexPath) so that it's not within the didSelectRowAtIndexPath method.

However, I can see a lot of repeated code here. That's what I am trying to minimise.

This question is more around MVC, responsibilities and fundamentally the way to structure my code.

Upvotes: 2

Views: 887

Answers (2)

Nitin Alabur
Nitin Alabur

Reputation: 5812

A simple way to get rid of your if/else statements is to create a function offlineDidSelectRowAtIndexPath that implements the switch case.

Here's how:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
  guard offlineMode != true else {
     offlineDidSelectRowAtIndexPath(tableview, didSelectRowAtIndexPath: indexPath)
     return 
  }

  switch indexPath.row {
   case 0:
    // do something else 1
   case 1:
    // do something else 2
   case 2:
    // do something else 3
   case 3:
    // do something else 4
   case 4:
    // do something else 5
   default:
    // default online and/or offline behaviour
}

and you'll have a function

func offlineDidSelectRowAtIndexPath(tableView: UITableView, indexPath: NSIndexPath) {

    switch indexPath.row {
     case 0:
        // do this 1
     case 1:
        // do this 2
     case 2:
        // do this 3
     case 3:
        // do this 4
     case 4:
        // do this 5
     default:
      // default behaviour
    }
}

Note: There may be much simpler / easier / better ways than my answer. (also, dont forget the break statements).

Upvotes: 1

Aaron
Aaron

Reputation: 7145

This is all high level and generalized, but hopefully helpful.

One approach to refactor that code is to put your data/state (which is what is likely represented by that big switch statement) into some other object, say a "model" and interrogate that model with the section/row input and return the expected output: maybe a cell of some type, perhaps. Or perhaps another model object specifically for the cell you need to create.

Another refactor is to use an enum to define your "modes" and use that in your switch statement as well as for the data that backs the table view sections and rows. Since enums are exhaustive in Swift you can eliminate the confusing default: case.

Another approach could be to represent each cell's state via a model object. Give the data to the model object and then pass the model object to the cell when you create it.

Anyway, there's lots of ways to skin this cat:

https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=Refactor+table+view

Upvotes: 1

Related Questions