ios - 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")
}
解决方案
通过使用下面提到的属性,您可以获得所有选定的行索引路径。
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
}
推荐阅读
- java - 使用不同的参数类型实现相同方法的抽象类 - Java/OOP
- r - r 循环遍历 data.frame 以使用不同的 DV 和权重运行相同的回归
- python - 将 int 加在一起并得到错误的分析器
- typescript - 我可以从 Typescript 中父类的构造函数触发子类的成员函数吗?
- r - sapply 在下面的示例中如何工作?
- python - Python mutiprocessing 进程不会终止
- wordpress - WordPress - 特色图片
- json - 使用 REST API 通过电子邮件限制获取 WooCommerce 优惠券
- swift - PolideaHow 可以将 Notify value 设置为 True on a specific Characteristic 和 Polidea/RXBluethootKit with IOS
- android - Volley 请求在 android studio 中返回相同的结果