gpt4 book ai didi

ios - swift 3/4 : SearchBar not filtering results properly in TableView

转载 作者:行者123 更新时间:2023-11-30 11:34:12 26 4
gpt4 key购买 nike

我有一个弹出窗口,顶部有 searchBar,下面有 TableView。 TableView 由动态数据填充。我有一个自定义 tableViewCell,带有名称标签和用于选择名称的复选框(M13CheckBox Library)。

现在,当我搜索名称时,首先当用户在搜索栏中键入名称时,不会加载 tableView。例如,假设有一些人名为“Mary”、“Mackenzie”、“Margaret”和“Mallory” ”。我想搜索“Margaret”,因此当我开始在搜索栏中输入“Mar”时,然后输入“Mary”和“ Margaret”在 tableView 中被正确过滤,但是当我返回时,即“Ma”,那么它应该显示所有 4 个名字,因为“Ma”是存在于列表中,但 tableView 不显示任何内容。

因此,如果名称中包含字母,则当用户在 searchBar 中键入内容时,tableView 应始终重新加载。请帮我解决这个问题。由于它是一个弹出窗口,我通过通知将数据从另一个 VC 传递到 tableView。这是我的搜索 VC 代码:

class ParticipantsListVC: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate{

public static var participantNameArray:[String] = [String]() //global var
var viewController: ViewController!

override func viewDidLoad() {
super.viewDidLoad()

tableView.delegate = self
tableView.dataSource = self
searchParticipantFilter.delegate = self

viewController = ViewController()

let notificationName = NSNotification.Name("reloadList")
NotificationCenter.default.addObserver(forName: notificationName, object: nil, queue: OperationQueue.main) { (notifObject) in
self.tableView.reloadData()
}
}

func searchBar(_ searchBar: UISearchBar, textDidChange searchText:String) {
if searchText == "" {
ParticipantsListVC.participantNameArray.removeAll()
viewController.getParticipantList() // func to get list from sever
}else{
ParticipantsListVC.participantNameArray = ParticipantsListVC.participantNameArray.filter({(name) -> Bool in
return name.lowercased().contains(searchText.lowercased())
})
}
self.tableView.reloadData()
}
}

此外,如果我选择一个名称,则该名称前面的复选框会被选中。但是当我单击搜索栏中的取消(X)时,总是显示选定的 tableView 中的第一个单元格,而不是我选择的名称。我不知道为什么在从过滤列表中选择名称后总是选择第一个单元格。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! ParticipantListCell

let dict = ParticipantsListVC.participantNameArray[indexPath.row]
cell.participantNameLabel.text = dict

if selectedIndexPaths.contains(indexPath) {
cell.selectedParticipantCB.setCheckState(.checked, animated: true)
}else{
cell.selectedParticipantCB.setCheckState(.unchecked, animated: true)
}
return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

// Since any random cell was getting selected on scrolling So I added this code.
tableView.deselectRow(at: indexPath, animated: true)
if selectedIndexPaths.contains(indexPath) {
selectedIndexPaths.removeObject(object: indexPath)
}else{
selectedIndexPaths.append(indexPath)
}
tableView.reloadData()
}

我不想在 headerView 或另一个 tableView 中使用 searchBar 来显示过滤列表。非常感谢。谢谢。

最佳答案

您需要创建另一个数组来保存数据数组的备份。

var arrParticipantList = [String]()
var arrParticipantListBackup = [String]()

override func viewDidLoad() {
super.viewDidLoad()

self.searchBar.delegate = self
self.tblParticipantList.delegate = self
self.tblParticipantList.dataSource = self
self.arrParticipantList = ["Mary", "Mackenzie", "Margaret", "Mallory","Molly"]
self.arrParticipantListBackup = self.arrParticipantList
}

用于搜索搜索字符串、重新填充数组和重新加载表格 View 的代码

extension ViewController: UISearchBarDelegate {

func searchBar(_ searchBar: UISearchBar, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {

var searchText = searchBar.text! + text
if range.length > 0 {

if range.location == 0 {

self.arrParticipantList = self.arrParticipantListBackup
self.tblParticipantList.reloadData()
return true
}
searchText = String(searchText.dropLast(range.length))
}
self.arrParticipantList = self.arrParticipantListBackup.filter({$0.lowercased().hasPrefix(searchText.lowercased())})
self.tblParticipantList.reloadData()
return true
}

func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {

self.searchBar.text = ""
self.searchBar.resignFirstResponder()
self.arrParticipantList = self.arrParticipantListBackup
self.tblParticipantList.reloadData()
}
}

表格 View 代码

extension ViewController: UITableViewDelegate, UITableViewDataSource {

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

return self.arrParticipantList.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCell(withIdentifier: "cell")
cell?.textLabel?.text = self.arrParticipantList[indexPath.row]
return cell!
}
}

希望这能解决您的问题。

关于ios - swift 3/4 : SearchBar not filtering results properly in TableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49935494/

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