搜索栏过滤问题 [英] Searchbar filtering issue

查看:53
本文介绍了搜索栏过滤问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新手,我正在尝试使用控制台中的搜索栏从数组中过滤名称,这是我在搜索栏中输入的内容,但使用谓词进行过滤却无法获得过滤名称...任何人都可以提供帮助这个问题

Im new to the swift, I am trying to filter name from an array using the search bar in console am getting what I entered in the search bar but filtering with predicate im not getting filtered name...please can anyone help in this issue

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

        }

    }

推荐答案

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

The most efficient way to filter custom classes is to use the same type for the data source array and the filtered array

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

添加属性isFiltering,该属性在搜索文本不为空时设置为true

Add a property isFiltering which is set to true when the search text is not empty

var isFiltering = false

并删除searchStringfilteredString

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

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

In updateSearchResults filter the data source array (with native Swift functions), set isFiltering accordingly and reload the table view

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

In the table view data source methods display the data depending on 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
}

这篇关于搜索栏过滤问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆