RichGibbs
RichGibbs

Reputation: 27

Sorting custom table view cells in Swift

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()
    }
}

Upvotes: 0

Views: 1279

Answers (1)

DonMag
DonMag

Reputation: 77423

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
}

Upvotes: 1

Related Questions