【发布时间】:2019-11-13 07:17:24
【问题描述】:
在 myscenario 中,我在 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
}
}
我的 Tableview 和 SearchBar 集合
@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()
}
【问题讨论】:
-
@vadian 你能检查一下这个问题吗?请给我一个答案
标签: ios swift uitableview searchbar