2017-07-08 10 views
1

こんにちはすべて、tableviewとserchbarcontrollerを使用してスコープ検索を設計しました。これを達成するために、以下のコードを使用しましたが、何とか実際の出力を返してくれません。助けを求めてありがとうございました。swift3でtableviewとsearchbarcontrollerを使用してスコープ検索を作成する方法

出力:

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

autocompletionのソースは、後でAPIから来るテキストフィールドに提供した値を意味します。 https://github.com/apasccon/SearchTextFieldなど、国のすべての名前はAPIに由来します。 – TheMiss

+0

学校、都市などの選択データソースで自動補完フィールドを生成したいですか? –

答えて

0

あなたはそれをこの方法を試すことができます:

  1. 自動補完テキストベースの配列を作成しますフィールド、データソースごとに1つ。
  2. これらのテキストフィールドを最初は非表示にします。
  3. 値を選択して検索ボタンをクリックした後、対応するテキストフィールドを表示させます。

問題が発生した場合は、ご意見をお寄せください。

+0

help.Butに感謝あなたが私を助けることができる場合私の更新された質問を参照してください。私のデザインを変更し、現在のデザインとコードで私の質問を更新しました。 – TheMiss

+0

@TheMiss '' 'updateSearchResults'''はどこで呼び出されますか? –

関連する問題