搜索栏过滤问题

Searchbar filtering issue

我是 swift 的新手,我正在尝试使用控制台中的搜索栏从数组中过滤名称我正在获取我在搜索栏中输入的内容,但使用谓词进行过滤我没有得到过滤名称.. .请有人能帮助解决这个问题

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{[=10=].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

        }

    }
let array = self.caseListOfBooker.filter{[=10=].person_of_interest.contains(searchString)}

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

的数组

失败了。您需要从 CaseDetails 对象获取字符串值并加入它们

使用

filteredString = array.map { [=11=].person_of_interest }

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

而不是

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

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

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

添加一个属性 isFiltering 当搜索文本不为空时设置为true

var isFiltering = false

并删除searchStringfilteredString

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

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

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{[=13=].person_of_interest.range(of: searchText, options: .caseInsensitive) != nil }
        isFiltering = true
    }
    tableview.reloadData()
}

在table中查看数据源方法根据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
}
var searchPredicate = NSPredicate()
searchPredicate = NSPredicate(format: "self CONTAINS[C] %@", your_searching_text)        
let Final_Search_array = (Your_Array).filtered(using: searchPredicate)