现在我可以使用您的方法 vadian 将复选标记添加到所选的选择表中。我还在我的数据类中添加了 Boolean("selected")
。我不明白的是如何使用 UserDefaults 保存为每行选择的 bool 值,或者如何将其加载到表中行的单元格中。我是否需要触摸 didload
部分?
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as? TableCell else {
return UITableViewCell()
}
let product = products[indexPath.row]
cell.accessoryType = product.selected ? .checkmark : .none
return cell }
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as? TableCell else {
return UITableViewCell()
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
var selected = products[indexPath.row].selected
products[indexPath.row].selected = !selected
tableView.reloadRows(at: [indexPath], with: .none)
if selected != true {print("selected", indexPath.row, selected )}
else {print("selected", indexPath.row, selected )}
selected = UserDefaults.standard.bool(forKey: "sound")
}
最佳答案
- 将属性
varroved : Bool
添加到您的数据模型 - 当选择更改时更新模型中的属性并重新加载行。
在
cellForRow
中根据属性设置复选标记... let cell = tableview.dequeueReusableCell(withIdentifier: "myfeedTVC", for: indexPath) as! MyFeedTVC let user = userDataArray[indexPath.row] cell.accessoryType = user.approved ? .checkmark : .none ...
关于ios - 在表格 View 中加载先前选择的行的复选标记(批准的用户),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47374467/