gpt4 book ai didi

Swift,带有 tableview 的弹出窗口在从未单击过的位置显示复选标记

转载 作者:行者123 更新时间:2023-11-28 13:43:57 24 4
gpt4 key购买 nike

我尝试显示一个弹出窗口,用户可以在其中选择多个行来过滤数据。为此,我创建了这个 popupViewcontroller:

 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if tableView.cellForRow(at: indexPath)?.accessoryType == UITableViewCell.AccessoryType.checkmark{
tableView.cellForRow(at: indexPath)?.accessoryType = UITableViewCell.AccessoryType.none

checkedCategories.remove(at: indexPath.row)
}else{
tableView.cellForRow(at: indexPath)?.accessoryType = UITableViewCell.AccessoryType.checkmark
checkedCategories.append(allcategories[indexPath.row])
}
}

但问题是如果用户点击第一个位置,它还会在最后一个项目处显示一个复选标记。此错误仅在滚动后发生。

最佳答案

不要使用单独的数组来保存索引路径。这是非常糟糕的做法。

细胞被重复使用。您必须确保每个 UI 元素都设置为 cellForRow 中定义的状态。

在您的数据模型中,最好在结构或类中添加一个属性

var isSelected = false

cellForRow中根据属性设置对勾

let item = datasource[indexPath.row]
cell.accessoryType = item.isSelected ? .checkmark : .none

didSelectRowAt 中切换 isSelected 并重新加载行

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {   
datasource[indexPath.row].isSelected.toggle()
tableView.reloadRows(at: [indexPath], with: .none)
}

关于Swift,带有 tableview 的弹出窗口在从未单击过的位置显示复选标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55678183/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com