0

I am working on an small project where I have an app that takes in tvshow information entered by the user and displays it in a custom tableview cell. I would like to sort the shows as they are entered based on which current episode the user is on. I know this code works because I tested it with print statements and it sorts the array but it does not sort on the simulator. So I just was curious where I should place this so that it sorts on the app side.

  func sortShows() {
        let sortedShows = tvShows.sorted { $0.currentEpisode > $1.currentEpisode}
        TVShowTableView.reloadData()
           print(sortedShows)
       }

Here is where I am currently placing it inside my view controller

extension TVShowListViewController: AddTVShowDelegate {
    func tvShowWasCreated(tvShow: TVShow) {
        tvShows.append(tvShow)
        dismiss(animated: true, completion: nil)
        TVShowTableView.reloadData()
        sortShows()
    }
}
4
  • Is TVShowTableView the instance of your table view? Or is the class? Instances (vars) should start with lower-case to help keep it straight. If it is the instance / var, try taking out TVShowTableView.reloadData() in your tvShowWasCreated() func, since you are also calling that after you've done your sort. Commented May 27, 2020 at 19:18
  • Yes it is the instance of my tableView and I removed the extra .reloadData, I also changed the casing on my tableview instance. Commented May 27, 2020 at 19:25
  • Did that fix your issue? Commented May 27, 2020 at 19:47
  • No it did not, the cells still don't update after each new show is added. Only in the compiler Commented May 27, 2020 at 19:49

1 Answer 1

1

In this part of your code:

func sortShows() {
    // here you are creating a NEW array
    let sortedShows = tvShows.sorted { $0.currentEpisode > $1.currentEpisode}
    // here you tell the table view to reload with the OLD array
    TVShowTableView.reloadData()
    print(sortedShows)
}

In your controller class, you probably have something like:

var tvShows: [TVShow] = [TVShow]()

and then you populate it with shows, like you do with a new show:

tvShows.append(tvShow)

Then your controller is doing something like:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "tvShowCell", for: indexPath) as! TVShowCell
    cell.tvShow = tvShows[indexPath.row]
    return cell
}

What you want to do is add another var to your class:

var sortedShows: [TVShow] = [TVShow]()

then change your sort func to use that array:

func sortShows() {
    // use the existing class-level array
    sortedShows = tvShows.sorted { $0.currentEpisode > $1.currentEpisode}
    // here you tell the table view to reload
    TVShowTableView.reloadData()
    print(sortedShows)
}

and change your other funcs to use the sortedShows array:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // use sortedShows array
    return sortedShows.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "tvShowCell", for: indexPath) as! TVShowCell
    // use sortedShows array
    cell.tvShow = sortedShows[indexPath.row]
    return cell
}

and you'll want to call sortShows() at the end of viewDidLoad() (or wherever you are getting your initial list of shows).

Edit

Another way you might use cellForRowAt:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "tvShowCell", for: indexPath) as! TVShowCell

    // use sortedShows array
    let tvShow = sortedShows[indexPath.row]
    cell.showTitleLable.text = tvShow.title
    cell.showDecriptionLable.text = tvShow.description

    return cell
}
Sign up to request clarification or add additional context in comments.

3 Comments

I have something very different in my cellForRowAt because I am passing three text fields into my tableview cell
@RichGibbs - it doesn't matter. The point is that you need to use your sorted array for your data, not your un-sorted array. See the Edit at the bottom of my answer.
Thank you. That second edit was closer to what I had so that made it work!! Thank you!!

Your Answer

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.