【问题标题】:Reload only section of tableview when expanding or not - Swift扩展或不扩展时仅重新加载表格视图的一部分 - Swift
【发布时间】:2020-03-28 12:11:18
【问题描述】:

我成功地用单元格折叠该部分,但最后我重新加载了所有的 tableview,我不希望这样。我想要的是仅在扩展或不扩展时重新加载该部分(插入/删除)。

这是我用来插入或删除部分行的方法:

func settingsViewTapped(view: SettingsCellView, section: Int) {
    var indexPaths = [IndexPath]()
    for row in self.settingsTrainings[section].workouts.indices {
        let indexPath = IndexPath(row: row, section: section)
        indexPaths.append(indexPath)
    }

    let isExpanded = self.settingsTrainings[section].isExpanded
    self.settingsTrainings[section].isExpanded = !isExpanded

    if isExpanded {
        tableView.deleteRows(at: indexPaths, with: .none)
        tableView.reloadData()
    } else {
        tableView.insertRows(at: indexPaths, with: .none)
        tableView.reloadData()
    }
}

【问题讨论】:

  • question非常相似或重复。
  • 我实现了很多次。 indexPaths 的插入和删除不需要 reloadData 。为什么要重新加载数据?

标签: ios objective-c swift


【解决方案1】:

你应该用 beginUpdates 和 endUpdates 包装 deleteRows 和 insertRows

tableView.beginUpdates()
tableView.insertRows(at: indexPaths, with: UITableViewRowAnimation.top)
tableView.endUpdates()

【讨论】:

    猜你喜欢
    • 2016-11-24
    • 1970-01-01
    • 1970-01-01
    • 2017-04-15
    • 2021-08-24
    • 1970-01-01
    • 1970-01-01
    • 2014-08-06
    • 1970-01-01
    相关资源
    最近更新 更多