首页 > 解决方案 > Swift 持久化并检索 UITableview 单元格复选标记

问题描述

我正在将JSON数据加载到 Tableview 中。我的表格视图允许多项选择checkmark。现在,我无法store选中单元格复选标记。怎么做?

NOTE:未来 JSON 数据数组计数可能会增加

我的表格视图代码

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell:TeamlistCustomCell = self.tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as! TeamlistCustomCell
        let textForRow = searching ? filteredData[indexPath.row] : membersData[indexPath.row]        
        cell.name.text = textForRow.firstname
        return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        self.tableView.deselectRow(at: indexPath, animated: true)
        let item = searching ? filteredData[indexPath.row] : membersData[indexPath.row]

        if selectedValues.contains(item) { //deselect
            selectedRows.remove(at: indexPath.row)
            tableView.cellForRow(at: indexPath)?.accessoryType = .none
            selectedValues.remove(item)
        } else {
            selectedRows.append(indexPath.row) //select
            tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark
            selectedValues.insert(item)
        }

        // Selected Row Index Store
        UserDefaults.standard.set(selectedRows, forKey: "SelectedIndexes")
    }

标签: iosswiftuitableview

解决方案


通过使用下面提到的属性,您可以获得所有选定的行索引路径。

var indexPathsForSelectedRows: [IndexPath]? { get }

例子:

let selectedRows = tableView.indexPathsForSelectedRows

下一步

重新加载 tableView 后,创建一个单独的方法并编写代码以选择所有先前选择的单元格。

func selectRow(at indexPath: IndexPath?, 
      animated: Bool, 
scrollPosition: UITableView.ScrollPosition)

例子

func updateTableState(tableView: UITableView) -> Void {

tableView.indexPathsForSelectedRows?.forEach({ (indexPath) in
            tableView.selectRow(at: indexPath, animated: true, scrollPosition: .none)
        })

}

您的代码与解决方案


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell:TeamlistCustomCell = self.tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as! TeamlistCustomCell
        let textForRow = searching ? filteredData[indexPath.row] : membersData[indexPath.row]
        cell.name.text = textForRow.firstname
        return cell
    }

    func updateTableState(tableView: UITableView) -> Void {
            tableView.indexPathsForSelectedRows?.forEach({ (indexPath) in
                tableView.selectRow(at: indexPath, animated: true, scrollPosition: .none)
            })
    }

    func reloadData() {
        tableView.reloadData()
        updateTableState(tableView: tableView)
    }

    func getAllSelectedObjects() -> [String]//mention whatever type array you have  {
        let selectedObjects = [String]() //mention whatever type array you have
        tableView.indexPathsForSelectedRows?.forEach({ (indexPath) in
            let object = searching ? filteredData[indexPath.row] : membersData[indexPath.row]
            selectedObjects.append(object)
        })
        return selectedObjects
    }

推荐阅读