Kodeco Forums

Enum-Driven TableView Development

In this tutorial, you will learn how to use Swift enums to handle the different states of your app to populate a table view.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/5542-enum-driven-tableview-development

Excellent tutorial.

I love the fact that you’re using values in the enum cases, then grabbing them in the switch statements. It’s a thing I don’t see a lot of swift devs using.

Thanks for a great tutorial.
For someone just beginning with Swift it has some great techniques.
Attaching [Recordings] to the .populated case is so clean and clever: I love it.
Not to mention reducing the reliance of checking for optionals.

Great tutorial. You increased my understanding of how enums with associated values can be used.

One of the best tutorials / code examples I’ve seen on RW. Bravo, Keegan!

Side Note: Swiftlint has major problems with adding any non-alpha characters in Enum names (such as an Array) - so if you use Swiftlint you’ll have to turn off those rules to get this project to build.

Hey Great one .

Please explain this

 func update(response: RecordingsResult) {
    if let error = response.error {
      state = .error(error)
      return
    }
    
    guard let newRecordings = response.recordings,
      !newRecordings.isEmpty else {
        state = .empty
        return
    }
    
    var allRecordings = state.currentRecordings
    allRecordings.append(contentsOf: newRecordings)
    
    if response.hasMorePages {
      state = .paging(allRecordings, next: response.nextPage)
    } else {
      state = .populated(allRecordings)
    }
  }

While paging What if Last page returns 0 elements then state = .empty will be executed and after that tableview will show empty view ? Can we fix this ?

@keeganrush Can you please help with this when you get a chance? Thank you - much appreciated! :]

Hey @prashantkt ,

The state is only set to .empty if there are no recordings. Only if response.recordings is nil, or if it is an empty array.

If there is a non-empty recordings array, but the RecordingsResult object has multiple pages, we enter the .paging state. Even if a particular page has 0 pages (which should never happen), you will still be in the .paging state, and the view will be populated with the recordings from the other pages.

I hope this helps! :]

This was just great! So much useful content that is immediately applicable. Thanks a lot RW team! Keep it coming!

@mett Really glad you enjoyed it! Cheers! :]

This tutorial is more than six months old so questions are no longer supported at the moment for it. Thank you!