如何在 UISearchController 中使用范围栏

Posted

技术标签:

【中文标题】如何在 UISearchController 中使用范围栏【英文标题】:How to use scope bar with UISearchController 【发布时间】:2015-05-15 07:47:28 【问题描述】:

我能够为 UISearchController 创建范围栏并设置它们的标题

    resultSearchController = (

        let controller = UISearchController(searchResultsController: nil)

        controller.searchResultsUpdater = self

        controller.dimsBackgroundDuringPresentation = false

        controller.searchBar.showsScopeBar = true

        controller.searchBar.scopeButtonTitles = ["One", "two", "three"]

        controller.searchBar.sizeToFit()

        self.tableView.tableHeaderView = controller.searchBar


        return controller

    )()

但是我实际上如何使用范围栏进行排序。我目前的排序方式如下

func updateSearchResultsForSearchController(searchController: UISearchController) 

    filteredTableData.removeAll(keepCapacity: false)

    let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %@", searchController.searchBar.text)

    let array = (tableData as NSArray).filteredArrayUsingPredicate(searchPredicate)

    filteredTableData = array as! [String]

    tableView.reloadData()


【问题讨论】:

请提供更多细节,你想要什么输出? 如果您看到我的代码,我可以设置范围栏标题。我想按三个类别对结果进行排序。 tableData 中是否有任何有助于使用范围栏进行过滤的数据? tableData 只是一个字符串数组,这将如何帮助按“一”、“二”或“三”进行过滤。 在上面提供的示例中,tableData 是字符串数组。但我想有类别,例如奇数、偶数、全数。这就是范围栏的标题。 【参考方案1】:

您需要添加搜索栏代理,如下所示。

class NumberTableViewController: UITableViewController, UISearchResultsUpdating, UISearchBarDelegate 

在视图中保存注册委托确实加载如下

resultSearchController.searchBar.delegate = self

Delegate 有方法告诉你按下了哪个范围栏按钮,如下所示

    func searchBar(searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) 

    filterArrayWithCharacterLength(selectedScope)


有关详细信息,请查看我在 How to add UISearchController to tableView 上的教程

【讨论】:

【参考方案2】:

即使晚了,它也可能对某人有所帮助! 假设您有一个对象 Person 和一个 filters 数组,其中保存了您已过滤的 People,那么

struct Person

  let name:String


func updateSearchResultsForSearchController(searchController:UISearchController)

    //Start filtering only when user starts to write something
    if searchController.searchBar.text.length > 0
    
        filteredPeople.removeAll(keepCapacity: false)
        self.filterContentForSearchText(searchController.searchBar.text)
        self.tableView.reloadData()
    
    else
    
        self.filteredPeople = self.people
        self.tableView.reloadData()
    


func filterContentForSearchText(searchText: String)

   self.filteredPeople = self.people.filter(( person: Person) -> Bool in

   //In this case we are searching for all, and the search is case insensitive      
   let categoryMatch = (scope == "All")
   let stringMatch = person.name.rangeOfString(searchText, options: NSStringCompareOptions.CaseInsensitiveSearch)
   return categoryMatch && (stringMatch != nil)
  )

【讨论】:

这个答案对我来说非常有用,只是一个问题:方法 filterContentForSearchText() 中的变量“范围”,它来自哪里?它被宣布无处... 我很抱歉! let scopes = self.searchDisplayController.searchBar.scopeButtonTitles as [String] let scope = scopes[0] // 假设第一个作用域是“All”,否则如果你有多个作用域,你会根据索引获得作用域。 感谢您的提示,但您竭尽全力解决问题。这就是为什么我没有给你答案。

以上是关于如何在 UISearchController 中使用范围栏的主要内容,如果未能解决你的问题,请参考以下文章

如何在 UISearchController 中使用范围栏

如何在 iOS 11 上删除 UISearchController 下的行?

如何在 tvOS 的同一视图中显示 UISearchController?

如何下移 UISearchController?

tvOS 14.3 UISearchController:如何锁定显示

如何在导航栏内隐藏 UISearchController