You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何重置UITableView中所有已选中的多选对勾标记?

实现方案

你的cell选中状态完全绑定了areas数组中对应元素的isSelected属性,重置逻辑只要批量修改该属性后刷新对应UI即可,以下是两种可行实现:

基础实现(适合数据量较小的场景)

直接全量修改选中状态后刷新整个列表,代码最简单:

@IBAction func resetButtonTapped(_ sender: UIBarButtonItem) {
    // 将所有区域的选中状态置为未选中
    areas.indices.forEach { areas[$0].isSelected = false }
    // 刷新列表更新所有行的UI
    myTableView.reloadData()
}

优化实现(适合数据量较大的场景)

仅收集之前选中的行进行局部刷新,减少不必要的UI渲染,性能更好:

@IBAction func resetButtonTapped(_ sender: UIBarButtonItem) {
    // 先收集所有当前处于选中状态的行的索引
    let selectedIndexPaths = areas.enumerated()
        .filter { $0.element.isSelected }
        .map { IndexPath(row: $0.offset, section: 0) }
    // 批量修改所有区域的选中状态
    areas.indices.forEach { areas[$0].isSelected = false }
    // 仅刷新之前选中的行即可完成UI更新
    myTableView.reloadRows(at: selectedIndexPaths, with: .none)
}

内容的提问来源于stack exchange,提问作者SeppMuskelMann

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.25 19:45:10