【问题标题】:Swift - Multiple TableView checkmark - save and load choiceSwift - 多个 TableView 复选标记 - 保存和加载选择
【发布时间】:2019-09-17 16:27:45
【问题描述】:

我正在尝试从 TableView 多个复选标记中保存和加载选项。

我已准备好代码,但我不知道如何保存选择。以及如何在列表开头加载选择。

我的密码:

var selectedCells = [IndexPath]()
var selectedAreas = [String]()
var Areas = [] //my text for the cells..



func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
    return Areas.count
}



func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell")!
    cell.textLabel?.text = Areas[indexPath.row]
    cell.accessoryType = selectedCells.contains(indexPath) ? .checkmark : .none
    return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let selectedCell = tableView.cellForRow(at: indexPath)
    selectedCells.append(indexPath)

    if selectedCell?.accessoryType == .checkmark {
        if let indexremove = selectedAreas.firstIndex(of: (Areas[indexPath.row])) {
            selectedAreas.remove(at: indexremove)
        }

        selectedCell?.accessoryType = .none
        print(selectedCells)
        print(selectedAreas)
        print("remove ")
        selectedCells = selectedCells.filter {$0 != indexPath}

    } else {
        print(selectedCells)
        print(selectedAreas)
        print("add")

        selectedAreas.append(Areas[indexPath.row])
        selectedCell?.accessoryType = .checkmark

    }
}

【问题讨论】:

  • 最简单的方法是将单元格的标识符(例如 indexPath)保存为 UserDefaults 中所选 indexPath 的数组并检索它们。到目前为止,它并不是理想的解决方案,因为它假定索引路径始终相同,并且底层数据模型永远不会改变。
  • 如果您能帮助我并在我的代码上更改它,我将非常高兴。
  • 非常可疑,同样的问题被不同的账户stackoverflow.com/questions/57973633/…

标签: ios arrays swift uitableview multi-select


【解决方案1】:

不要使用多个数组作为数据源。这是非常糟糕的做法,效率低下。

删除它们

罢工>

var selectedCells = [IndexPath]()
var selectedAreas = [String]()  

Area 声明为结构并添加isSelected 成员

struct Area {
    let name : String
    var isSelected : Bool

    init(name : String, isSelected : Bool = false) {
        self.name = name
        self.isSelected = isSelected
    }
}

var areas = [Area(name: "Foo"), Area(name: "Bar")]

cellForRowAt 中根据isSelected 分配复选标记

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
    let area = areas[indexPath.row]
    cell.textLabel?.text = area.name
    cell.accessoryType = area.isSelected ? .checkmark : .none
    return cell
}

didSelectRow 切换isSelected 并重新加载该行(是的,只有两行代码)

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    areas[indexPath.row].isSelected.toggle()
    tableView.reloadRows(at: [indexPath], with: .none)
}

你得到选定的区域

let selectedAreas = areas.filter{$0.isSelected}

还有一个名字数组

let selectedAreaNames = areas.filter{$0.isSelected}.map{$0.name}

要将名称加载并保存到UserDefaults,请添加这两个方法

func saveSelection()
{
   let selectedAreaNames = areas.filter{$0.isSelected}.map{$0.name}
   UserDefaults.standard.set(selectedAreaNames, forKey: "selectedNames")
}

func loadSelection()
{
   guard let selectedAreaNames = UserDefaults.standard.array(forKey: "selectedNames") as? [String] else { return }
   for (index, area) in areas.enumerated() {
      areas[index].isSelected = selectedAreaNames.contains(area.name)
   }
   tableView.reloadData()
}

【讨论】:

  • 完美!非常感谢你的帮助!!只是一个问题,如何将选择保存到 UserDefault,然后从那里加载?
  • 您可以保存所选项目的名称。
  • 我意识到了。如何使选择 (selectedAreas) 显示为列表标记?
  • 我不明白。
  • 我想用 userdefault 保存选择,这样打开应用后选择会显示为列表标记
猜你喜欢
  • 2015-01-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-06
  • 1970-01-01
相关资源
最近更新 更多