【问题标题】:Sorting custom table view cells in Swift在 Swift 中对自定义表格视图单元格进行排序
【发布时间】:2020-05-27 19:11:38
【问题描述】:

我正在做一个小项目,我有一个应用程序接收用户输入的电视节目信息并将其显示在自定义表格视图单元格中。我想根据用户正在播放的当前剧集对节目进行排序。我知道这段代码有效,因为我用 print 语句对其进行了测试,它对数组进行了排序,但它没有在模拟器上排序。所以我只是好奇我应该把它放在哪里,以便它在应用程序端进行排序。

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

这是我目前将其放置在视图控制器中的位置

extension TVShowListViewController: AddTVShowDelegate {
    func tvShowWasCreated(tvShow: TVShow) {
        tvShows.append(tvShow)
        dismiss(animated: true, completion: nil)
        TVShowTableView.reloadData()
        sortShows()
    }
}

【问题讨论】:

  • TVShowTableView 是您的表格视图的实例吗?还是?实例(vars)应该以小写开头,以帮助保持直截了当。如果它实例/变量,请尝试在您的tvShowWasCreated() func 中取出TVShowTableView.reloadData(),因为您在完成排序后也会调用它。
  • 是的,它是我的 tableView 的实例,我删除了额外的 .reloadData,我还更改了我的 tableview 实例的大小写。
  • 这解决了您的问题吗?
  • 不,没有,每次添加新节目后,单元格仍然不会更新。仅在编译器中

标签: ios arrays swift xcode user-interface


【解决方案1】:

在这部分代码中:

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

在你的控制器类中,你可能有类似的东西:

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

然后你用节目填充它,就像你用一个新节目一样:

tvShows.append(tvShow)

然后你的控制器正在做类似的事情:

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
}

你想要做的是在你的类中添加另一个变量:

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

然后更改您的排序函数以使用该数组:

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

并更改您的其他函数以使用 sortedShows 数组:

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
}

您需要在viewDidLoad() 结束时致电sortShows()(或在您获得初始节目列表的任何地方)。

编辑

你可能使用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
}

【讨论】:

  • 我的 cellForRowAt 中有一些非常不同的东西,因为我将三个文本字段传递到我的 tableview 单元格中
  • @RichGibbs - 没关系。关键是您需要将 排序数组 用于数据,而不是未排序数组。请参阅我的答案底部的编辑
  • 谢谢。第二次编辑更接近我所拥有的,所以它可以工作!!谢谢!!
猜你喜欢
  • 2017-11-06
  • 2017-05-25
  • 1970-01-01
  • 2017-02-28
  • 2014-11-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多