在表视图中快速搜索ALL,包括表视图单元格详细信息

时间:2016-11-18 07:17:40

标签: ios swift

我刚接触Swift。我在视图控制器中有一个表视图,包括4个表视图单元格。我想创建一个搜索栏,可以搜索所有属性,包括4个表格单元格,如表格行的标签名称,状态,时间,请求名称。我一直在研究许多搜索教程,但它只教导只搜索一个属性,而不是状态,时间和请求名称。

是否有任何指南或教程可供遵循?感谢您的阅读时间。

//This is what i edit, because i want it to search all attributes 
func updateSearchResults(for searchController: UISearchController) {
    filtered.removeAll(keepingCapacity: false)
    let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %@", searchController.searchBar.text!)
    let array1 = arrStatus + arrProcessName
    let array = (array1 as NSArray).filtered(using: searchPredicate)
    filtered = array as! [String]
    tableView?.reloadData()
}

//This is without edit according to online tutorial, it seems like it only can search one attributes
func updateSearchResults(for searchController: UISearchController) {
    filtered.removeAll(keepingCapacity: false)
    let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %@", searchController.searchBar.text!)
    let array = (arrStatus as NSArray).filtered(using: searchPredicate)
    filtered = array as! [String]
    tableView?.reloadData()
}




 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    removeProgressBar()
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! HistoryCustomCell

    cell.selectionStyle = .none
    cell.accessoryType = UITableViewCellAccessoryType.none

    if self.searchCtrler.isActive {
    //In here, the value will be overrides by the search text, 
    //the search cannot be perform because it just 
    //replaced the label instead of searching. 
        cell.lbl_status.text = filtered[indexPath.row]
    } else {
        cell.lbl_request_name.text = arrProcessName[indexPath.row]
        cell.lbl_requested_by.text = "Requested by: " + arrRequestedBy[indexPath.row]
        cell.lbl_creation_time.text = arrCreationTime[indexPath.row]
        cell.lbl_status.text = arrStatus[indexPath.row]

        switch arrStatus[indexPath.row] {
            case "Completed", "Approved":
                cell.img_status.image = UIImage(named: "ic_approved")
            case "Running", "Processing", "Pending":
                cell.img_status.image = UIImage(named: "ic_pending")
            case "Denied", "Rejected":
                cell.img_status.image = UIImage(named: "ic_reject")
            case "Error":
                cell.img_status.image = UIImage(named: "ic_terminated")
            case "Retracted":
                cell.img_status.image = UIImage(named: "ic_retracted")
            default:
                cell.img_status.image = UIImage(named: "")
        }
    }

    return cell
}

0 个答案:

没有答案