ios - 在 `UISearchbar` 中搜索名称或号码
问题描述
在tableViewCell
我有userNameLbl
名字,userClgLbl
有数字。我想在tableView
名称搜索或数字搜索中搜索和显示数据。
如果用户搜索名称 - 基于名称,我可以在 tableView 中显示数据。
如果用户搜索数字 - 基于数字我可以在 tableView 中显示数据。
但是如何同时使用名称和编号来处理单个搜索栏。实际上,这里我的数据是来自服务器的动态数据,并且号码不是电话号码。
UISearchBarDelegate
添加到我的班级
let searchBar = UISearchBar()
var filteredData: [Any]!
@IBOutlet weak var listTblView: UITableView!
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return filteredData.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// create a new cell if needed or reuse an old one
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! TableViewCell
cell.userNameLbl.text = filteredData[indexPath.row] as? String
cell.userClgLbl.text = clg_uniq[indexPath.row] as? String
return cell
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
let strArr:[String] = clg_uniq as! [String]
filteredData = searchText.isEmpty ? clg_uniq : strArr.filter({(dataString: String) -> Bool in
// If dataItem matches the searchText, return true to include it
return dataString.range(of: searchText, options: .caseInsensitive) != nil
})
DispatchQueue.main.async {
self.listTblView.reloadData()
}
if searchText == "" {
DispatchQueue.main.async {
searchBar.resignFirstResponder()
}
}
}
//Added these lines after json parsing
self.filteredData = self.clg_uniq
self.listTblView.reloadData()
我的示例 JSON 数据是
{"log" = (
{
Name = "Name1";
"clg_uniq" = 5c640e7b86e35;
},
{
Name = "Name2";
"clg_uniq" = <null>;
},
{
Name = <null>;
"clg_uniq" = 5c647af5d5c4d;
},
{
Name = "Name4";
"clg_uniq" = 5c647a0427253;
},
{
Name = <null>;
"clg_uniq" = <null>;
},
{
Name = "Name6";
"clg_uniq" = $cuniq";
},
)
}
解决方案
添加以下变量 -
var logArray = [Dictionary<String, Any>]() // For all result
var searchedLogArray = [Dictionary<String, Any>]() // For filtered result
var searchActive = false // whenever user search anything
替换 UISearchBarDelegate
-
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
searchActive = searchText.count > 0 ? true : false
let namePredicate = NSPredicate(format: "Name CONTAINS[c] %@", searchText)
let clgUniqPredicate = NSPredicate(format: "clg_uniq CONTAINS[c] %@", searchText)
let compoundPredicate = NSCompoundPredicate.init(orPredicateWithSubpredicates: [namePredicate, clgUniqPredicate])
searchedLogArray = logArray.filter({
return compoundPredicate.evaluate(with: $0)
})
listTblView.reloadData()
}
替换 UITableViewDataSource
-
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return searchActive ? searchedLogArray.count : logArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// create a new cell if needed or reuse an old one
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! TableViewCell
let logDict = searchActive ? searchedLogArray[indexPath.row] : logArray[indexPath.row]
// Name
if let name = log["Name"] as? String{
cell.userNameLbl.text = name
}else{
cell.userNameLbl.text = ""
}
// clg_uniq
if let clgUniq = log["clg_uniq"] as? String {
cell.userClgLbl.text = clgUniq
}else{
cell.userClgLbl.text = ""
}
return cell
}
我希望你正在回复Dictionary<String, Any>
如果您仍有任何问题,请告诉我。
推荐阅读
- terraform - Terraform:为多个实例创建 CloudWatch 警报时出错
- angular - 尝试过滤 *ngFor 指令中的数据
- javascript - 我可以判断屏幕是否关闭与浏览器最小化或标签是否已用 js 更改?
- angular - Angular Material Button Toggle 在布局/视图之间切换
- python - 如何找到 numpy 轴上的最终累积总和?
- rust - 如何为使用生命周期实现的特征传递函数?
- swagger - 大摇大摆的模式数组
- for-loop - 嵌套 For 循环 astrix 模式
- firebase - 在查询中使用 FieldPath 设置 redux-firestore 监听器
- python-3.x - 在ubuntu上没有pip的python安装