在Tableview中为先前选定的行(已批准的用户)加载检查标记

时间:2017-11-19 07:00:49

标签: ios swift tableview userdefaults

现在,我可以使用您的方法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")
}

1 个答案:

答案 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  
    ...