【发布时间】:2017-01-20 08:06:06
【问题描述】:
我需要实现一个 searchBar 来搜索和过滤带有 2 个标签的 tableview。标记来自 2 个不同数组的数据。因此,当我通过数组 1/标签 1 过滤时,它会过滤但标签 2 保持不变,因此结果是混合的。这两个数组都是在 SQL Query 结果后创建的,包含 2 列数据。我的意思是 arr1[0] 和 arr2[0] 是同一行但不同的列。经过太多次尝试后我被卡住了。这是最新的代码:
var arr1 = [String]()
var arr2 = [String]()
var filtered:[String] = []
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if(searchActive) {
return filtered.count
} else {
return arr1.count
}
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> xxTableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "xxCell", for: indexPath) as! xxTableViewCell
if(searchActive){
cell.label1.text = filtered[(indexPath as NSIndexPath).row]
} else {
cell.label1.text = arr1[(indexPath as NSIndexPath).row]
cell.label2.text = arr2[(indexPath as NSIndexPath).row]
}
return cell
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
filtered = arr1.filter({ (text) -> Bool in
let tmp: NSString = text as NSString
let range = tmp.range(of: searchText, options: NSString.CompareOptions.caseInsensitive)
return range.location != NSNotFound
})
if(filtered.count == 0){
searchActive = false;
} else {
searchActive = true;
}
self.tableView.reloadData()
}
【问题讨论】:
标签: ios arrays swift uitableview uisearchbar