gpt4 book ai didi

ios - 清除搜索结果后,Swift Tableview 搜索结果单元格选择复选标记不保存

转载 作者:行者123 更新时间:2023-11-29 05:19:18 25 4
gpt4 key购买 nike

在我的场景中,我在 codable 的帮助下将 JSON 数据列出到 Tableview 中。在这里,我有一个 Tableview 和带有过滤器数组的 searchBar 。此外,还实现了带有复选标记选项的表格 View 多单元格选择。在这里,无需搜索所选单元格复选标记persistant工作正常,但如果我搜索特定数据并选择单元格,复选标记会显示,但在清除搜索结果(fileterarray到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
}
}

我的 Tableview 和 SearchBar 集合

@IBOutlet weak var searchbar: UISearchBar!
@IBOutlet var tableView: UITableView!
var searching = false
var membersData = [TeamListData]()
var filteredData = [TeamListData]()

我的 Tableview 委托(delegate)

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}
}

SearchBar 委托(delegate)

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)
}

关于ios - 清除搜索结果后,Swift Tableview 搜索结果单元格选择复选标记不保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58832058/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com