gpt4 book ai didi

ios - UISearchController 结果 TableViewController 在 UISearchBar 为空时不清除结果

转载 作者:行者123 更新时间:2023-12-01 19:32:37 24 4
gpt4 key购买 nike

我不擅长 iOS 开发,并且遇到了一个简单的问题。

我正在尝试创建一个 UISearchController,它在首次启动或 UISearchBar 为空时在结果 tableView 的 backgroundView 中有一条空消息(无论是退格,点击 x 清除字段,还是取消 UISearchBar) .

当 UISearchBar 第一次聚焦时,我已经完成了添加空消息,但是在搜索发生后,当我取消搜索并清空 UISearchBar 后,旧的搜索结果仍然在结果 TableView 中。

我认为一直都是这样,但是我之前没有看到旧的结果,而现在我设置了 searchController.searchResultsController?.view.isHidden = false能够在 Controller 首次启动时看到背景。

我已经进行了广泛的搜索,只是无法弄清楚如何清除旧结果。

我的整个结果 TableViewController 代码:

import UIKit
import MapKit
import Mapbox

class LocationSearchTableViewController: UITableViewController {

var matchingItems: [MKMapItem] = []
var mapView: MGLMapView? = nil

// This somehow connects and pleases the delegate
var handleMapSearchDelegate: HandleMapSearch? = nil

// Address formatting, not entirely sure what this is doing
func parseAddress(selectedItem: MKPlacemark) -> String {
// put a space between "4" and "Melrose Place"
let firstSpace = (selectedItem.subThoroughfare != nil && selectedItem.thoroughfare != nil) ? " " : ""
// put a comma between street and city/state
let comma = (selectedItem.subThoroughfare != nil || selectedItem.thoroughfare != nil) && (selectedItem.subAdministrativeArea != nil || selectedItem.administrativeArea != nil) ? ", " : ""
// put a space between "Washington" and "DC"
let secondSpace = (selectedItem.subAdministrativeArea != nil && selectedItem.administrativeArea != nil) ? " " : ""
let addressLine = String(
format:"%@%@%@%@%@%@%@",
// street number
selectedItem.subThoroughfare ?? "",
firstSpace,
// street name
selectedItem.thoroughfare ?? "",
comma,
// city
selectedItem.locality ?? "",
secondSpace,
// state
selectedItem.administrativeArea ?? ""
)
return addressLine
}
}

// Extension with UISearchResultsUpdating delegate protocols
extension LocationSearchTableViewController: UISearchResultsUpdating {
func updateSearchResults(for searchController: UISearchController) {

// This unhides the results view so it's visible when the field is focused
searchController.searchResultsController?.view.isHidden = false

// Get searchbar text and make query
guard let _ = mapView,
let searchBarText = searchController.searchBar.text else { return }
let request = MKLocalSearchRequest()
request.naturalLanguageQuery = searchBarText
let search = MKLocalSearch(request: request)

search.start { response, _ in
guard let response = response else {
return
}
self.matchingItems = response.mapItems
self.tableView.reloadData()
}

if searchBarText == "" {
// searchResults.removeAll()
tableView.reloadData()
}
}
}

// Extension with all UITableViewDataSource methods grouped together
extension LocationSearchTableViewController {

// Create rows based on number of returning items
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if matchingItems.count == 0 {
setEmptyMessage()
} else {
restore()
}


return matchingItems.count
}


// Populate cells
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell")!
let selectedItem = matchingItems[indexPath.row].placemark
cell.textLabel?.text = selectedItem.name
cell.detailTextLabel?.text = parseAddress(selectedItem: selectedItem)

return cell
}
}

// Groups UITableViewDelegate methods together
extension LocationSearchTableViewController {

// Function that fires on tapping a row
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

// Assign placemark value (coordinates)
let selectedItem = matchingItems[indexPath.row].placemark


// Delegate function that passes placemark
handleMapSearchDelegate?.dropPin(placemark: selectedItem)

// Dismiss searchController
dismiss(animated: true, completion: nil)
}
}

extension LocationSearchTableViewController {

func setEmptyMessage() {
let rect = CGRect(origin: CGPoint(x: 0,y :0), size: CGSize(width: self.view.bounds.size.width, height: self.view.bounds.size.height))
let messageLabel = UILabel(frame: rect)
messageLabel.text = "Empty"
messageLabel.textColor = .black
messageLabel.numberOfLines = 0
messageLabel.textAlignment = .center
messageLabel.font = UIFont(name: "TrebuchetMS", size: 15)
messageLabel.sizeToFit()

self.tableView.backgroundView = messageLabel
// self.tableView.backgroundView?.isHidden = false
self.tableView.backgroundColor = Colors.black1
self.tableView.separatorStyle = .none
}

func restore() {
self.tableView.backgroundView = nil
self.tableView.separatorStyle = .singleLine
}
}

以及实现 UISearchController 的主 ViewController 中的相关(我认为)UISearchBar 代码:
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {

if let navigationController = navigationController { // this unwraps navigation controller
navigationController.setNavigationBarHidden(true, animated: true)
UIView.animate(withDuration: 1) {
self.placeholderView.alpha = 0
}
}
}

最佳答案

这是清除 dataArray .. 并调用 reloadData() 所需的方法在您的 tableView

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

searchResults.removeAll()
tableView.reloadData()

}
}

关于ios - UISearchController 结果 TableViewController 在 UISearchBar 为空时不清除结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61579878/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com