TableView 结果错误
TableView result error
我有一个 Search Bar
和一个 UITableView
,如果我在搜索栏中搜索某些内容,它将在 table 视图中打印结果。
如果我搜索像 "Name 01" 这样的名字,然后单击这个名字来获取信息,稍后我重新打开搜索栏并尝试搜索像 "Name 02" 这样的其他名字,我会看到"Name 01" 导致 Table 视图,我不知道如何清除它。
我也尝试过刷新Table视图,但没有成功。
问题视频:https://streamable.com/98j0w
代码是这样
extension LocationSearchTable : UISearchResultsUpdating {
func updateSearchResults(for searchController: UISearchController) {
//print("updateSearchResults")
if searchController.searchBar.text == nil {
seenNames.removeAll()
matchingItems.removeAll()
self.tableView.reloadData()
}
guard let mapView = mapView,
let searchBarText = searchController.searchBar.text else { return }
let request = MKLocalSearchRequest()
request.naturalLanguageQuery = searchBarText
request.region = mapView.region
let search = MKLocalSearch(request: request)
search.start { response, _ in
guard let response = response else {
return
}
for (index , name) in response.mapItems.enumerated() {
let item = response.mapItems[index]
if(checkIfItemExistInDatabase(key: String(item.name!)) != nil && !seenNames.contains(name.name!)){
matchingItems.append(item)
seenNames.insert(name.name!)
self.tableView.reloadData()
}
}
}
}
}
我希望如果我进行研究,table带有搜索栏文本的查看结果会被清除并且不会显示之前的结果
而不是:
if searchController.searchBar.text == nil {
...
...
试试
let searchText = searchController.searchBar.text
if searchText == nil || searchText.isEmpty {
...
...
还有一件事,就在
之前
search.start { response, _ in
添加
matchingItems.removeAll()
tableView.reloadData()
我有一个 Search Bar
和一个 UITableView
,如果我在搜索栏中搜索某些内容,它将在 table 视图中打印结果。
如果我搜索像 "Name 01" 这样的名字,然后单击这个名字来获取信息,稍后我重新打开搜索栏并尝试搜索像 "Name 02" 这样的其他名字,我会看到"Name 01" 导致 Table 视图,我不知道如何清除它。
我也尝试过刷新Table视图,但没有成功。
问题视频:https://streamable.com/98j0w
代码是这样
extension LocationSearchTable : UISearchResultsUpdating {
func updateSearchResults(for searchController: UISearchController) {
//print("updateSearchResults")
if searchController.searchBar.text == nil {
seenNames.removeAll()
matchingItems.removeAll()
self.tableView.reloadData()
}
guard let mapView = mapView,
let searchBarText = searchController.searchBar.text else { return }
let request = MKLocalSearchRequest()
request.naturalLanguageQuery = searchBarText
request.region = mapView.region
let search = MKLocalSearch(request: request)
search.start { response, _ in
guard let response = response else {
return
}
for (index , name) in response.mapItems.enumerated() {
let item = response.mapItems[index]
if(checkIfItemExistInDatabase(key: String(item.name!)) != nil && !seenNames.contains(name.name!)){
matchingItems.append(item)
seenNames.insert(name.name!)
self.tableView.reloadData()
}
}
}
}
}
我希望如果我进行研究,table带有搜索栏文本的查看结果会被清除并且不会显示之前的结果
而不是:
if searchController.searchBar.text == nil {
...
...
试试
let searchText = searchController.searchBar.text
if searchText == nil || searchText.isEmpty {
...
...
还有一件事,就在
之前search.start { response, _ in
添加
matchingItems.removeAll()
tableView.reloadData()