Swift坚持并检索UITableview单元格复选标记

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

NOTE: JSON数据数组的数量将来可能会增加

我的Tableview代码

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")
    }
ccdelan 回答:Swift坚持并检索UITableview单元格复选标记

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

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,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
    }
本文链接:https://www.f2er.com/3161328.html

大家都在问