【发布时间】:2021-12-30 14:27:16
【问题描述】:
我有一个带有复选标记的多选表视图。
然后我在导航栏中有一个“全部重置”按钮。我想用这个按钮清除(删除/重置)所有复选标记。
首先我制作的结构和数组:
struct Area {
let name : String
var isSelected : Bool
init(name : String, isSelected : Bool = false) {
self.name = name
self.isSelected = isSelected
} }
var areas = [Area(name: "Name1"), Area(name: "Name2"), Area(name:"Name3"), Area(name: "Name4"), Area(name: "Name5")]
在“didSelectRowAt”我有这个:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
areas[indexPath.row].isSelected.toggle()
tableView.reloadRows(at: [indexPath], with: .none) }
在“cellForRowAt”我有这个:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = myTableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! MyTableViewCell
let area = areas[indexPath.row]
cell.textLabel?.text = area.name
cell.accessoryType = area.isSelected ? .checkmark : .none }
然后我的导航栏中有一个“全部重置”按钮:
@IBAction func resetButtonTapped(_ sender: UIBarButtonItem)
{//...}
我想用这个按钮重置所有选中的复选标记。我不知道该怎么做。
有人可以帮我解决这个问题吗?
【问题讨论】:
-
基本上,表格视图是基于某种数组的 data (在您的示例中,我猜是
areas)。因此,只需将所有isSelected设置为 false 并在您的表格视图上调用reloadData()。 -
尽量使用class而不是struct,因为class是引用类型和struct值类型。
标签: swift xcode tableview removeall checkmark