2017-06-26 16 views
0

SearchBarの検索結果をtableViewに表示したいのですが、わかりません。テーブルビューでデータを読み込む方法検索バースウィフトの結果

enter image description here

私は、TextFieldの宛先を入力すると、すべてがあるだけで正常に動作しますが、私は、検索バーに入力したときに動作していない:

enter image description here

をここで呼び出す方法です[文字列]で検索するにはFUNC:

extension ViewController: UITextFieldDelegate { 


public func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { 

    let substring = (textField.text! as NSString).replacingCharacters(in: range, with: string) 

    DestinoP.searchDestinos(substring) 

    return true 

}} 

ここでは、結果を表示する方法です。

extension ViewController: UITableViewDataSource { 

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as UITableViewCell 
    let index = indexPath.row as Int 

    isSearching = true // this is just for SearchBar 
    if isSearching { 
     if let str = autoCompleteDestino[index].desDestino { 
      cell.textLabel?.text = str 
     } 
    } 

    return cell 
}} 

拡張子のViewController:UITableViewDelegate {

public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 

    if isSearching { // this is just for SearchBar 
     return autoCompleteDestino.count 
    } 

    return autoCompleteDestino.count 

} 

public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 

    let selectedCell: UITableViewCell = tableView.cellForRow(at: indexPath)! 

    destinoSearch.text = selectedCell.textLabel!.text! 
    busquedaDestinosText.text = selectedCell.textLabel!.text! 

}} 

これは私が検索バーに入力したときに結果を表示しようとする方法である:

extension ViewController: UISearchBarDelegate { 

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) { 

    let stringOfUser = (busquedaDestinosText.text) 

    DestinoP.searchDestinos(stringOfUser!) 

    if destinoSearch.text == "" { 
     isSearching = false 
     view.endEditing(true) 
     tableView.reloadData() 
     print(isSearching) 
    } else { 
     isSearching = true 
     print(isSearching) 
     print(autoCompleteDestino) 
    } 

}} 

は私を助けることができますか?で開始する

答えて

0

、あなたは 'は、2つのクラス内の配列originalfiltered

var originalData: [String]! 
var filteredData: [String]! 

があなたのクラス

let searchController = UISearchController(searchResultsController: nil) 

セットアップであなたのsearchControllerをsearchControllerを追加し、tableViewとしてsearchBarを追加する必要がありますsのヘッダ

searchController.dimsBackgroundDuringPresentation = false 
searchController.searchBar.delegate = self 
searchController.searchBar.placeholder = "Search" 
searchController.searchBar.sizeToFit() 
searchController.hidesNavigationBarDuringPresentation = false 

tblView.tableHeaderView = searchController.searchBar 

searchBar実装のdelegate方法最後に

func searchBarCancelButtonClicked(_ searchBar: UISearchBar) { 
    searchController.searchBar.text = "" 
    tblView.reloadData() 
} 

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) { 
    let searchString = searchController.searchBar.text 
    filteredData = originalData.filter({ (item) -> Bool in 
     let value: NSString = item as NSString 
     return (value.range(of: searchString!, options: .caseInsensitive).location != NSNotFound) 
    }) 
    tblView.reloadData() 
} 

を、あなたのtableViewdelegate方法は、この

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    if searchController.isActive == true && searchController.searchBar.text != "" { 
     return filteredData.count 
    } 
    return originalData.count 
} 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    if searchController.isActive == true && searchController.searchBar.text != "" { 
     //RETURN CELLS CREATED FROM FILTERED DATA 
    } 
    //RETURN CELLS CREATED FROM ORIGINAL DATA 
} 

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    if searchController.isActive == true && searchController.searchBar.text != "" { 
     //HANDLE ROW SELECTION FROM FILTERED DATA 
    } 
    //HANDLE ROW SELECTION FROM ORIGINAL DATA 
} 
+0

こんにちはMalik..thanks!この場合、ストーリーボードに検索バーを追加します。@IBOutlet weak var destinoSearch:UISearchBar! (プログラムではない)。そして、 "filteredDataまたはautoCompleteDestino"を私の場合に表示する方法は次のとおりです:if str = autoCompleteDestino [index] .desDestino { cell.textLabel?.text = str }検索バーに入力するとautoCompleteDestinoを表示する方法は? – xhinoda

+1

あなたは依然としてsearchBar 'delegate'メソッドにフックする必要があります。これらのメソッドは、データをフィルタリングし、 'tableView'をリロードします。 – Malik

0

OK ...ので、私は私の問題を解決するようになります。 @マリクは私の心を開いている! (ありがとう!) 最終的に私はこれを行うことにより、プログラム的に検索バーを置く:私はUISearchBarDelegateを使用

tableView.tableHeaderView = searchController.searchBar 

:私のテーブルビューで

let searchController = UISearchController(searchResultsController: nil) 
    searchController.dimsBackgroundDuringPresentation = false 
    searchController.searchBar.delegate = self 
    searchController.searchBar.placeholder = "Buscar Destinos" 
    searchController.searchBar.sizeToFit() 
    searchController.hidesNavigationBarDuringPresentation = false 

やショーを

                   `extension ViewController: UISearchBarDelegate { 
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) { 

    let stringOfUser = (searchController.searchBar.text!) 

    DestinoP.searchDestinos(stringOfUser) 

}} 

はのtableViewで結果を表示します:

extension ViewController: UITableViewDataSource { 
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as UITableViewCell 
    let index = indexPath.row as Int 

     if let str = autoCompleteDestino[index].desDestino { 
      cell.textLabel?.text = str 
     } 

    return cell 
}} 

とのtableViewのreloadData:

func mostarResultados(ArrayResultados: [Destinos]) { 

    autoCompleteDestino = ArrayResultados 
    tableView.reloadData() 
} 

ありがとう!

関連する問題