0

我有兴趣使用 tableView 根据搜索栏中的输入列出可能的地址。选择包含所需地址的单元格后,搜索栏文本包含地址,但是我希望可能的地址(单元格)消失。didSelectRowAt 中的 self.searchResultsTableView.reloadData() 是否清除所有单元格或是否有其他命令?我不确定如何在选择适当的地址后清除单元格而不进行迭代并让建议引入更多单元格。

import UIKit
import MapKit

class SearchViewController: UIViewController {

@IBOutlet weak var searchBar: UISearchBar!
    var searchCompleter = MKLocalSearchCompleter()
    var searchResults = [MKLocalSearchCompletion]()
    var searchSource: [String]?



@IBOutlet weak var searchResultsTableView: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    searchCompleter.delegate = self
    searchBar.delegate = self
}

}


extension SearchViewController: UISearchBarDelegate {

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

    searchCompleter.queryFragment = searchText
}
}

extension SearchViewController: MKLocalSearchCompleterDelegate {

func completerDidUpdateResults(_ completer: MKLocalSearchCompleter) {
    searchResults = completer.results
    searchResultsTableView.reloadData()
}

func completer(_ completer: MKLocalSearchCompleter, didFailWithError error: Error) {
    // handle error
}
}

extension SearchViewController: UITableViewDataSource {

func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return searchResults.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let searchResult = searchResults[indexPath.row]
    let cell = UITableViewCell(style: .subtitle, reuseIdentifier: nil)
    cell.textLabel?.text = searchResult.title
    cell.detailTextLabel?.text = searchResult.subtitle

    return cell
}
}

extension SearchViewController: UITableViewDelegate {

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    tableView.deselectRow(at: indexPath, animated: true)

    let completion = searchResults[indexPath.row]

    let searchRequest = MKLocalSearchRequest(completion: completion)
    let search = MKLocalSearch(request: searchRequest)
    search.start { (response, error) in
        let coordinate = response?.mapItems[0].placemark.coordinate
        print(String(describing: coordinate))
        print(response?.mapItems)
        self.searchBar.text = response?.mapItems[0].name
    }


    self.searchResultsTableView.reloadData()


}
}
4

1 回答 1

1

如果要清除 tableView,则需要使数据源数组为空,然后重新加载 tableView。

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    tableView.deselectRow(at: indexPath, animated: true)

    let completion = searchResults[indexPath.row]

    let searchRequest = MKLocalSearchRequest(completion: completion)
    let search = MKLocalSearch(request: searchRequest)
    search.start { (response, error) in
        let coordinate = response?.mapItems[0].placemark.coordinate
        print(String(describing: coordinate))
        print(response?.mapItems)
        self.searchBar.text = response?.mapItems[0].name
    }

    //Make empty your array ant then reload tableView
    searchResults = []
    self.searchResultsTableView.reloadData()
}
于 2017-05-24T07:28:57.627 回答