swift - 尝试在 Swift 中使用 UISearchResult。如何实现过滤?

标签 swift search filtering uisearchcontroller

我正在尝试在我的应用程序中创建露营地搜索功能。我希望用户能够在搜索栏中输入内容,并让显示的露营地过滤器列表与他们输入的内容相匹配。现在,用户点击按钮即可调出 SearchCampgroundsViewController.swift。这是我迄今为止在该文件中的代码:

import UIKit

class SearchCampgroundsViewController: UIViewController, UISearchResultsUpdating {

    let searchController = UISearchController(searchResultsController: nil)

    @IBOutlet weak var tableView: UITableView!
    var campgrounds: [Campground]? {
       return DataProvider.sharedInstance.allCampground()
    }


    override func viewDidLoad() {
        super.viewDidLoad()

        searchController.searchResultsUpdater = self
        searchController.hidesNavigationBarDuringPresentation = false
        searchController.dimsBackgroundDuringPresentation = false
        self.tableView.tableHeaderView = searchController.searchBar
    }


    func updateSearchResultsForSearchController(searchController: UISearchController) {

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

extension SearchCampgroundsViewController: UITableViewDataSource {

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.campgrounds?.count ?? 0
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("campgroundCell")! as UITableViewCell
        cell.textLabel?.text = campgrounds?[indexPath.row].facilityName ?? "NO NAME FOR THIS FACILITY"
        return cell
    }
}

露营地列表在表格 View 中正确显示(列表长约 4000 个项目),并且搜索栏正确显示在表格顶部。我可以在搜索栏中输入内容,但结果不会被过滤。如何实现过滤? updateSearchResultsForSearchController 函数需要添加哪些内容?

最佳答案

您需要使用在搜索栏中输入的文本 (searchController.searchBar.text) 过滤数据源 (campgrounds),然后使用以下命令重新加载表格 View 过滤后的数据。

如果您希望将数据源保留为计算属性,则需要定义一个新变量来保存已过滤露营地的数组(见下文)。然后,您必须引入检查以确定要使用哪个数据源,具体取决于搜索 Controller 是否处于事件状态。

或者,您可以使用单个数据源,该数据源根据需要返回一组已过滤或未过滤的露营地。

请注意,您需要导入 Foundation 才能使用 rangeOfString()

尝试按照以下方式进行操作:

import Foundation

/* ... */

var filtered: [Campground]?

func updateSearchResultsForSearchController(searchController: UISearchController) {        
    filtered = campgrounds?.filter {
        // True if facilityName contains search bar text (case-sensitive).
        $0.facilityName.rangeOfString(searchController.searchBar.text!) != nil
    }

    tableView.reloadData()
}

/* ... */

extension SearchCampgroundsViewController: UITableViewDataSource
{
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if searchController.active && !searchController.searchBar.text.isEmpty {
            return filtered?.count ?? 0
        }

        return campgrounds?.count ?? 0
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("campgroundCell")! as UITableViewCell

        if searchController.active && searchController.searchBar.text != "" {
            cell.textLabel?.text = filtered?[indexPath.row].facilityName ?? "NO NAME FOR THIS FACILITY"
        } else {
            cell.textLabel?.text = campgrounds?[indexPath.row].facilityName ?? "NO NAME FOR THIS FACILITY"
        }

        return cell
    }
}

关于swift - 尝试在 Swift 中使用 UISearchResult。如何实现过滤?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36448421/

相关文章:

search - Box.com 搜索 API 用于检索指定文件夹中的所有文件

c - 搜索哈希表不起作用,for 循环不在 c 中执行

javascript - Kendo UI 无法过滤网格表

javascript - 使用 HTML 输入在 D3 中进行动态过滤

regex - swift 2 : How do I split a string without loosing the characters it split on?

swift - 扩展 NSDecimalNumber 以符合 ExpressibleByStringLiteral 会导致 LLDB RPC 服务器崩溃

php - 如何向 html 表格的列添加搜索和过滤器?

SQL vs NoSQL,用于添加多个过滤器后将呈现给用户的数据

swift - 使用从父类(super class)继承的值创建字典

ios - 更新 UISplitView Controller 的两侧