[英]Swift Tableview search result cell click to checkmark not showing
[英]Swift Tableview search result cell selection checkmark not saving after clearing the search result
在 myscenario 中,我在 codable 的帮助下codable
JSON
数据列出到 Tableview 中。 在这里,我有一个带有过滤器数组的 Tableview 和searchBar
。 此外,使用复选checkmark
选项实现了 tableview 多个单元格选择。 在这里,没有搜索选定的单元格复选标记persistant
工作正常,但如果我搜索特定数据并选择单元格,复选标记正在显示,但在清除搜索结果(fileterarray to tableviewarray)
后复选标记未显示(其平均选定的单元格复选标记未保留且未显示清除搜索结果后)。 我在代码库中的某个地方犯了错误。 请更正它并提供一个可靠的解决方案。
实际操作和问题是需要允许用户通过勾选启用和不搜索来选择他们的单元格。 没有搜索一切正常,但搜索选择的检查不持久......
JSON 可编码
// MARK: - TeamListRoot
struct TeamListRoot: Codable {
let status: Bool
let data: [TeamListData]
}
// MARK: - TeamListData
struct TeamListData: Codable, Hashable {
let userid: String?
let firstname, designation: String?
let profileimage: String?
var isSelected = false
private enum CodingKeys : String, CodingKey {
case userid, firstname, designation, profileimage
}
}
我的表格视图和搜索栏 Collections
@IBOutlet weak var searchbar: UISearchBar!
@IBOutlet var tableView: UITableView!
var searching = false
var membersData = [TeamListData]()
var filteredData = [TeamListData]()
我的 Tableview 代表
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return searching ? filteredData.count : membersData.count
}
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.empName.text = textForRow.firstname
cell.designationLabel.text = textForRow.designation
cell.accessoryType = textForRow.isSelected ? .checkmark : .none
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}
}
搜索栏代表
func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
self.searchbar.setShowsCancelButton(true, animated: true)
searching = true
}
func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
self.searchbar.setShowsCancelButton(false, animated: true)
searching = false
}
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
searching = false
self.searchbar.resignFirstResponder()
self.searchbar.text = ""
filteredData.removeAll()
self.tableView.reloadData()
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
searching = false
self.searchbar.resignFirstResponder()
self.searchbar.text = ""
filteredData.removeAll()
self.tableView.reloadData()
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
if !searchText.isEmpty {
filteredData = membersData.filter({ ($0.firstname?.localizedCaseInsensitiveContains(searchText))! })
searching = true
} else {
filteredData.removeAll()
searching = false
}
tableView.reloadData()
}
Swift 5
//替换你的数组声明
var membersData: [TeamListData] = [TeamListData]()
var filteredData: [TeamListData] = [TeamListData]()
//替换didSelectRowAt代码
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.tblSearch.deselectRow(at: indexPath, animated: true)
if searching == false {
if let row = self.membersData.firstIndex(where: { $0.userid == membersData[indexPath.row].userid }) {
membersData[row].isSelected.toggle()
}
} else {
if let row = self.membersData.firstIndex(where: { $0.userid == filteredData[indexPath.row].userid }) {
membersData[row].isSelected.toggle()
}
if let row = self.filteredData.firstIndex(where: { $0.userid == filteredData[indexPath.row].userid }) {
filteredData[row].isSelected.toggle()
}
}
tableView.reloadRows(at: [indexPath], with: .none)
}
您在搜索时切换的是来自过滤数据的 select。 完成后,从 membersData 加载项目。 在 membersData 你没有选择行
改变这个:
if searching == false {
membersData[indexPath.row].isSelected.toggle()
} else {
filteredData[indexPath.row].isSelected.toggle()
}
对此:
membersData[indexPath.row].isSelected.toggle()
filteredData[indexPath.row].isSelected.toggle()
我的解决方案是在 WillDisplay 单元格中重新加载 tableView 后设置选择。
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
let contact = contactsFiltered[indexPath.row]
if selectedContacts.contains(contact) {
tableView.selectRow(at: indexPath, animated: false, scrollPosition: .none)
} else {
tableView.deselectRow(at: indexPath, animated: false)
}
}
我只需要让我的联系人过滤器保持最新
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let contactToAppend = contactsFiltered[indexPath.row]
selectedContacts.append(contactToAppend)
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
if let contactCell = tableView.cellForRow(at: indexPath) as? ContactTVCell {
if let contactToDelete = contactCell.contact {
selectedContacts.removeAll(where: { $0 == contactToDelete})
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.