如何在iOS swift3中使用SearchBar过滤detailTextLabel数据

时间:2018-07-19 08:49:18

标签: ios swift swift3

我正在使用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;
}

0 个答案:

没有答案