ios - 在表格 View 中加载先前选择的行的复选标记(批准的用户)

标签 ios swift tableview userdefaults

现在我可以使用您的方法 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/

相关文章:

ios - Model类获取对象但不满足TableView

ios - 修改 NSFetchRequest 生成 NSRangeException 索引 0 超出空数组的范围

ios - 如何在 Swift 中将 SKScene 覆盖在 SCNScene 上?

ios - 通过 NSAttributedText 使用的图标的分辨率

ios - iOS 钥匙串(keychain)数据是否在重新安装应用程序后仍然存在?

ios - 获取 UIStackView 中的第一个 UIButton

iOS:导航栏中的 UIActivityIndi​​cator 没有停止

ios - Swift 中的多个泛型类继承

Swift 在 View 中滑动时启用 TableView 重新加载

java - TableView 项目未删除但不响应