问题描述
我解析了一个 JSON 数组,它给了我 [[String:String]]
然后我把它放到一个表视图中.
I have parsed a JSON array which gives me [[String:String]]
which I then put into a table view.
我想使用 UISearchBar
来搜索数据,但遇到了麻烦,因为我不确定如何处理 [[String:String]]
格式.
I want to use a UISearchBar
to be able to search the data, but am having trouble because I'm not sure how to deal with the [[String:String]]
format.
我尝试创建另一个变量,将数组数据保存为 [String]
并且我可以过滤它,但我无法正确显示结果.对不起,如果这有点令人困惑,因为我在过去的几个小时里试图弄清楚它让自己感到困惑.谢谢!
I have tried creating another variable which holds the array data as [String]
and I can filter it, but I can't display the results correctly. Sorry if this is a bit confusing as i have confused myself the last couple of hours trying to figure it out. Thank you!
import UIKit
import Alamofire
import SwiftyJSON
class StartViewController: UIViewController, UISearchBarDelegate, UITableViewDelegate, UITableViewDataSource {
var names = [
[String: String]
]()
var isSearching = false
var filteredData = [String]()
@IBOutlet weak
var tableView: UITableView!
@IBOutlet weak
var searchBar: UISearchBar!
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell",
for: indexPath)
let name = names[indexPath.row]
let text: String!
// here I get 'Cannot assign value of type '[String : String]' to type 'String?''
if isSearching {
text = filteredData[indexPath.row]
} else {
text = nil
}
cell.textLabel ? .text = name["name"]
cell.textLabel ? .textColor = UIColor.blue
return cell
}
}
这里有两个部分
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
if searchBar.text == nil || searchBar.text == "" {
isSearching = false
view.endEditing(true)
tableView.reloadData()
} else {
isSearching = true
// and here I get 'Binary operator '==' cannot be applied to operands of type '[String : String]' and 'String?''
filteredData = names.filter({
$0 == searchBar.text
})
tableView.reloadData()
}
}
感谢您的帮助.
//如果有帮助的话,这是我的解析函数
edit:// here is my parsing function if that helps at all
func parse(json: JSON) {
for result in json[].arrayValue {
let name = result["name"].stringValue
let obj = ["name": name]
names.append(obj)
}
tableView.reloadData()
}
}
推荐答案
为了能够显示完整数据和过滤数据,两个数组的类型必须相同.
To be able to display both full and filtered data both arrays must be of the same type.
var filteredData = [[String:String]]()
在 cellForRow
中使用依赖于 isSearching
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell",
for: indexPath)
let text : [String:String]
if isSearching {
text = filteredData[indexPath.row]
} else {
text = names[indexPath.row]
}
cell.textLabel?.text = text["name"]
cell.textLabel?.textColor = UIColor.blue
return cell
}
在 searchBar:searchDidChange
中,您必须通过一个键进行过滤,例如
In searchBar:searchDidChange
you have to filter by a key for example
filteredData = names.filter({
$0["name"] == searchBar.text
})
但是我建议使用自定义类或结构而不是字典.
However I recommend to use a custom class or struct rather than a dictionary.
这篇关于如何使用 UISearchBar 过滤类型为 [[String:String]] 的数组并显示为 String的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!