现在,我可以使用您的方法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")
}
答案 0 :(得分:0)
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
...