我正在使用SearchBar过滤联系人表。我正在使用名称字段进行搜索。它过滤名称而不是手机号码。如何在iOS Swift中同时过滤手机号码和相应的姓名
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
filtered = stringArray.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.myTable.reloadData()
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:UITableViewCell = myTable.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
if(searchActive){
cell.textLabel?.text = filtered[indexPath.row]
cell.detailTextLabel?.text = numArray[indexPath.row]
} else {
cell.textLabel?.text = stringArray[indexPath.row];
cell.detailTextLabel?.text = numArray[indexPath.row]
}
return cell;
}