搜索栏过滤问题

时间:2019-05-14 09:56:05

标签: ios swift4 uisearchbar

我是新手,我正在尝试使用控制台中的搜索栏从数组中过滤名称,这是我在搜索栏中输入的内容,但使用谓词进行过滤却无法获得名称的过滤条件...请有人帮忙这个问题

var caseListOfBooker:[CaseDetails]=[]

var searchString:String=""

var filteredString = [String]()

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
            print("searchText \(searchText)")

            searchString = searchText
            updateSearchResults()
            tableview.reloadData()
        }
        func updateSearchResults(){

        filteredString.removeAll(keepingCapacity: false)

        let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %@", searchString)

        let array = self.caseListOfBooker.filter{$0.person_of_interest.contains(searchString)}
            print(array)

                    if let list=array as? [String]{
                        filteredString=list
                    }


            print(filteredString)

            tableview.reloadData()



    }


extension SearchPOIVC : UITableViewDelegate, UITableViewDataSource {

 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if filteredString != []{

            return filteredString.count
        }
        else
        {
            if searchString != "[]" {
                return caseListOfBooker.count
            }else {
                return 0
            }
        }

    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 80.00
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell:POIProfileDetailsCell = tableview.dequeueReusableCell(withIdentifier: "POIProfileDetailsCell", for: indexPath) as! POIProfileDetailsCell
        if filteredString != []{

            cell.poiName.text = filteredString[indexPath.row]

            return cell
        }else{

            if searchString != "[]"{
                 cell.poiName.text = self.caseListOfBooker[indexPath.row].person_of_interest

            }
            return cell

        }

    }

3 个答案:

答案 0 :(得分:1)

let array = self.caseListOfBooker.filter{$0.person_of_interest.contains(searchString)}

您正在获取CaseDetails对象的数组,并尝试转换为String的数组

失败。您需要从CaseDetails对象获取字符串值并将其加入

使用

filteredString = array.map { $0.person_of_interest }

for caseDetail in array {
    filteredString.append(caseDetail.person_of_interest)
}

代替

if let list = array as? [String]{
    filteredString=list
}

答案 1 :(得分:1)

过滤自定义类的最有效方法是对数据源数组过滤后的数组使用相同类型

var caseListOfBooker = [CaseDetails]()
var filteredBooker = [CaseDetails]()

添加属性isFiltering,当搜索文本不为空时将其设置为true

var isFiltering = false

并删除searchStringfilteredString

var searchString:String=""
var filteredString = [String]()

updateSearchResults中过滤数据源数组(使用本机Swift函数),相应地设置isFiltering并重新加载表视图

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
    print("searchText \(searchText)")
    updateSearchResults(searchText: searchText)
}

func updateSearchResults(searchText: String) {
    if searchText.isEmpty {
        filteredBooker.removeAll()
        isFiltering = false
    } else {
        filteredBooker = caseListOfBooker.filter{$0.person_of_interest.range(of: searchText, options: .caseInsensitive) != nil }
        isFiltering = true
    }
    tableview.reloadData()
}

在表格视图中,数据源方法根据isFiltering

显示数据
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return isFiltering ? filteredBooker.count : caseListOfBooker.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableview.dequeueReusableCell(withIdentifier: "POIProfileDetailsCell", for: indexPath) as! POIProfileDetailsCell
    let booker = isFiltering ? filteredBooker[indexPath.row] : caseListOfBooker[indexPath.row]
    cell.poiName.text = booker.person_of_interest
}

答案 2 :(得分:0)

var searchPredicate = NSPredicate()
searchPredicate = NSPredicate(format: "self CONTAINS[C] %@", your_searching_text)        
let Final_Search_array = (Your_Array).filtered(using: searchPredicate)