2015-05-15 28 views
7

我能夠創建範圍酒吧UISearchController &設置它們的標題如何使用範圍酒吧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() 

} 
+0

請給出更多的細節,你想要什麼輸出? – Mukesh

+0

如果您看到我的代碼,我可以設置範圍欄標題。我想按三個類別對結果進行排序。 –

+0

你有'tableData'中的任何數據,這將有助於使用範圍欄進行過濾嗎? 'tableData'只是一個字符串數組,如何幫助用「one」,「two」或「three」過濾。在上面提供的例子中的 –

回答

0

您需要添加搜索欄委託如下。鑑於

class NumberTableViewController: UITableViewController, UISearchResultsUpdating, UISearchBarDelegate { 

保存報名參會做負載如下

resultSearchController.searchBar.delegate = self 

代表有方法來告訴你哪些範圍欄按鈕被按下如下

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

    filterArrayWithCharacterLength(selectedScope) 

} 

有關詳細信息,請查看我的教程on How to add UISearchController to tableView

1

即使晚了,它可能會幫助別人! 假設你有對象者和filteredPeople數組,你留人你過濾,然後

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) 
    }) 
} 
+0

這個答案在這個時候對我來說非常有用,只是一個問題:方法filterContentForSearchText()中的變量「scope」,它來自哪裏?它被宣佈無處... –

+1

我很抱歉! '讓示波器= self.searchDisplayController.searchBar.scopeButtonTitles爲[字符串]'' 讓範圍=範圍[0]' //假設第一個範圍「全部」,否則,你得到一個基於索引的範圍如果你有多個範圍。 – Bettimms

+0

謝謝你的提示,但你給了一半驢試圖解決問題。這就是爲什麼我沒有給你答案。 –