现在我可以在选择的表中添加复选标记,使用您的方法vadian。我还在数据类中添加了Boolean("selected")
。我没有得到的是如何使用 UserDefaults 保存为每一行选择的布尔值,或者如何将其加载到表中的行的单元格中。我甚至需要触摸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")
}
- 向数据模型添加属性
var approved : Bool
- 当选择更改时,更新模型中的属性并重新加载行。
-
在
cellForRow
中,根据属性设置复选标记... let cell = tableview.dequeueReusableCell(withIdentifier: "myfeedTVC", for: indexPath) as! MyFeedTVC let user = userDataArray[indexPath.row] cell.accessoryType = user.approved ? .checkmark : .none ...