SearchBarの検索結果をtableViewに表示したいのですが、わかりません。テーブルビューでデータを読み込む方法検索バースウィフトの結果
私は、TextFieldの宛先を入力すると、すべてがあるだけで正常に動作しますが、私は、検索バーに入力したときに動作していない:
をここで呼び出す方法です[文字列]で検索するには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)
}
}}
は私を助けることができますか?で開始する
こんにちはMalik..thanks!この場合、ストーリーボードに検索バーを追加します。@IBOutlet weak var destinoSearch:UISearchBar! (プログラムではない)。そして、 "filteredDataまたはautoCompleteDestino"を私の場合に表示する方法は次のとおりです:if str = autoCompleteDestino [index] .desDestino { cell.textLabel?.text = str }検索バーに入力するとautoCompleteDestinoを表示する方法は? – xhinoda
あなたは依然としてsearchBar 'delegate'メソッドにフックする必要があります。これらのメソッドは、データをフィルタリングし、 'tableView'をリロードします。 – Malik