Swift - 多个 TableView 复选标记 - 保存和加载选择

Swift - Multiple TableView checkmark - save and load choice

我正在尝试从 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 {[=11=] != indexPath}

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

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

    }
}

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

删除它们

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{[=14=].isSelected}

和一组名称

let selectedAreaNames = areas.filter{[=15=].isSelected}.map{[=15=].name}

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

func saveSelection()
{
   let selectedAreaNames = areas.filter{[=16=].isSelected}.map{[=16=].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()
}