2017-07-08 38 views
1

大家好,都設計了一個使用tableview和serchbarcontroller的範圍搜索。爲了達到這個目的,使用了下面的代碼,但不知怎的,它並沒有返回實際輸出。希望得到幫助。謝謝。如何使用tableview和searchbarcontroller在swift3中創建範圍搜索

輸出:

here is my output's ScreenShot

代碼:

import UIKit 

class SearchBookVC: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchResultsUpdating, UISearchBarDelegate { 

@IBOutlet weak var tableview: UITableView! 

struct Books { 
    var name = String() 
    var board = String() 
    var classname = String() 
    var area = String() 
    var city = String() 
    } 

    var books = [Books(name: "Physics", board: "CBSE",classname:"XI",area:"Karnataka",city:"Bangalore"), 
       Books(name:"English",board:"CBSE",classname:"X",area:"Maharashtra",city:"pune"), 
       Books(name:"biology",board:"IB",classname:"XII",area:"Gujarat",city:"Rajkot"), 

       Books(name:"chemistry",board:"IB",classname:"X",area:"Gujarat",city:"Ahmedabad"), 

       Books(name:"Maths",board:"ICSE",classname:"IX",area:"Maharashtra",city:"Mumbai"), 
       Books(name:"Science",board:"ICSE",classname:"XII",area:"Karnataka",city:"Mysore") 
       ] 
    var filteredBooks = [Books]() 

    let searchController = UISearchController(searchResultsController: nil) 



    override func viewDidLoad() { 
    super.viewDidLoad() 

     filteredBooks = books 

     searchController.searchResultsUpdater = self 
     searchController.dimsBackgroundDuringPresentation = false 
     definesPresentationContext = true 
     tableview.tableHeaderView = searchController.searchBar 

     searchController.searchBar.scopeButtonTitles = ["All","Name", "Board", "Class", "Area","City"] 

     searchController.searchBar.delegate = self 

     self.tableview.register(mycell.self, forCellReuseIdentifier: "cell") 


    // Do any additional setup after loading the view. 
} 

func applySearch(searchText: String, scope: String = "All") { 


    if searchController.searchBar.text! == "" 
    { 
     filteredBooks = books.filter { book in 
      let nameofbook = (scope == "All") || (book.name == scope) || (book.board == scope) || (book.classname == scope) || (book.area == scope) || (book.city == scope) 
      return nameofbook 
    } 
    } 
     else 
     { 
      filteredBooks = books.filter { book in 
       let nameofbook = (scope == "All") || (book.name == scope) || (book.board == scope) || (book.classname == scope) || (book.area == scope) || (book.city == scope) 
       return nameofbook && book.name.lowercased().contains(searchText.lowercased()) && book.board.lowercased().contains(searchText.lowercased()) && book.classname.lowercased().contains(searchText.lowercased()) && book.area.lowercased().contains(searchText.lowercased()) && book.city.lowercased().contains(searchText.lowercased()) 
      } 
     } 
    self.tableview.reloadData() 
} 
func updateSearchResults(for searchController: UISearchController) { 

    let searchBar = searchController.searchBar 
    let selectedScope = searchBar.scopeButtonTitles![searchBar.selectedScopeButtonIndex] 
    applySearch(searchText: searchController.searchBar.text!,scope: selectedScope) 
} 
func searchBar(_ searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) { 
    applySearch(searchText: searchController.searchBar.text!,scope: searchBar.scopeButtonTitles![selectedScope]) 
} 



func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return self.filteredBooks.count 
} 
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! mycell 

    cell.bookname?.text = self.filteredBooks[indexPath.row].name 
    cell.Boardname?.text = self.filteredBooks[indexPath.row].board 
    cell.classtitle?.text = self.filteredBooks[indexPath.row].classname 

    return cell 
} 
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    print("Row \(indexPath.row) selected") 
} 

} 
+0

對不起,並沒有真正瞭解你的情況。你的意思是你想使用你選擇的文字作爲自動完成文本字段的數據源嗎? –

+0

自動完成的來源意味着我們提供給文本字段的值稍後將來自API。例如https://github.com/apasccon/SearchTextField國家的所有名稱都將來自API。 – TheMiss

+0

所以你想通過選擇數據源如學校,城市等來生成自動完成字段? –

回答

0

你可以試試這樣:

  1. 製作自動完成文本的數組領域,一個用於每個數據源。
  2. 首先讓這些文本字段不可見。
  3. 選擇值並單擊搜索按鈕後,讓相應的文本字段可見。

不確定它是否能滿足您的要求,如果您有任何問題,請留下評論。

+0

感謝您的幫助。但請參閱我更新的問題,如果你能幫助我out.I改變了我的設計和更新我的問題與當前的設計和代碼。 – TheMiss

+0

@TheMiss哪裏調用updateSearchResults'''? –