在一种情况下,我将借助JSON
将codable
数据列出到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}
}
SearchBar代表
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()
}
答案 0 :(得分:0)
正在搜索时,您可以从filteredData中进行选择。 完成后,您将从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()
答案 1 :(得分:0)
快速5
//替换您的 Array 声明
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)
}
答案 2 :(得分:0)
我的解决方案是在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})
}
}
}