[英]Swift Tableview search result cell click to checkmark not showing
[英]Swift Tableview search result selected cell checkmark not persisting
我正在将数据从我的JSON
加载到UITableView
中。 在这里,Tableview 我实现了多个单元格选择复选标记选项。 我将两个数组用于正常数据加载,另一个用于过滤数组数据。 在这里,我附加的每一个选择都是isSelected
标记持久性的。 带有复选标记单元格选择的正常数据加载工作正常。 但是,每当我尝试搜索并选择显示在搜索结果中的复选标记但清除搜索数据后,我无法在表格视图中看到搜索选择的复选标记。
我正在使用两个数组,一个用于normal
数据加载,另一个用于 tableview search
数据。 搜索清除后如何实现此checkmark
可用性。
我的代码
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return searching ? filteredData.count : membersData.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:listCustomCell = self.tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as! listCustomCell
let textForRow = searching ? filteredData[indexPath.row] : membersData[indexPath.row]
cell.empName.text = textForRow.firstname
cell.designationLabel.text = textForRow.designation
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.tableView.deselectRow(at: indexPath, animated: true)
if searching == false {
membersData[indexPath.row].isSelected.toggle()
} else {
filteredData[indexPath.row].isSelected.toggle()
}
tableView.reloadRows(at: [indexPath], with: .none)
let selectedAreas = membersData.filter{$0.isSelected}
let selectedfilterAreas = filteredData.filter{$0.isSelected}
print("\(selectedAreas)")
print("\(selectedfilterAreas)")
}
您的逻辑不正确,因为您没有更新在搜索关闭时显示数据的数据源。 解决方案是当您切换过滤后的数组时,请同时切换您的 membersData 的原始数据源数组。,。 必须有一个区分员工的关键,因此当您在搜索时 select 员工时更新该员工的 membersData isSelected。
编辑太多:p
eq - // 我假设 emp_id 是唯一键。 membersData 也是可变的
CODE -
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.tableView.deselectRow(at: indexPath, animated: true)
if searching == false {
membersData[indexPath.row].isSelected.toggle()
} else {
filteredData[indexPath.row].isSelected.toggle()
let empId = filterdData[indexPath.row].emp_id
for (index,obj) in membersData.enumerated() {
membersData[index].isSelected = obj.emp_id == empId ? filteredData[indexPath.row].isSelected : obj.isSelected
}
tableView.reloadRows(at: [indexPath], with: .none)
let selectedAreas = membersData.filter{$0.isSelected}
let selectedfilterAreas = filteredData.filter{$0.isSelected}
print("\(selectedAreas)")
print("\(selectedfilterAreas)")
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.