ios - Swift - UISearchController Results displayed Large spacing between search bar and TableView Cells
问题描述
Sorry for the image being large I haven't posted on SO in a while. Anyways, my UISearchController displayed results create a large spacing between the searchbar and tableview cells. This is a very inconsistent bug. What is the correct way to stop this spacing from existing when displaying search results? Thank you!
Edit: Where the inconsistency comes into play is that if you begin a search and then Cancel and then being another search the UITableViewCells hug the searchbar as they should without the mass spacing.
UISearchController Display TableView
import UIKit
import MapKit
class CustomerAddSettingsLocationSearch: UITableViewController {
weak var handleMapSearchDelegate: CustomerAddSettingsLocationSeaarching?
var matchingItems: [MKMapItem] = []
var mapView: MKMapView?
func parseAddress(selectedItem:MKMapItem) -> String {
// put a space between "4" and "Melrose Place"
let firstSpace = (selectedItem.placemark.subThoroughfare != nil &&
selectedItem.placemark.thoroughfare != nil) ? " " : ""
// put a comma between street and city/state
let comma = (selectedItem.placemark.subThoroughfare != nil || selectedItem.placemark.thoroughfare != nil) &&
(selectedItem.placemark.subAdministrativeArea != nil || selectedItem.placemark.administrativeArea != nil) ? ", " : ""
// put a space between "Washington" and "DC"
let secondSpace = (selectedItem.placemark.subAdministrativeArea != nil &&
selectedItem.placemark.administrativeArea != nil) ? " " : ""
let addressLine = String(
format:"%@%@%@%@%@%@%@",
// street number
selectedItem.placemark.subThoroughfare ?? "",
firstSpace,
// street name
selectedItem.placemark.thoroughfare ?? "",
comma,
// city
selectedItem.placemark.locality ?? "",
secondSpace,
// state
selectedItem.placemark.administrativeArea ?? ""
)
return addressLine
}
}
extension CustomerAddSettingsLocationSearch: UISearchResultsUpdating {
func updateSearchResults(for searchController: UISearchController) {
guard let mapView = mapView,
let searchBarText = searchController.searchBar.text else { return }
let request = MKLocalSearch.Request()
request.naturalLanguageQuery = searchBarText
request.region = mapView.region
let search = MKLocalSearch(request: request)
search.start { response, _ in
guard let response = response else {
return
}
self.matchingItems = response.mapItems
self.tableView.reloadData()
}
}
}
extension CustomerAddSettingsLocationSearch {
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return matchingItems.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell")!
let selectedItem = matchingItems[indexPath.row]
cell.textLabel?.text = selectedItem.name
cell.detailTextLabel?.text = parseAddress(selectedItem: selectedItem)
return cell
}
}
extension CustomerAddSettingsLocationSearch {
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let selectedItem = matchingItems[indexPath.row]
handleMapSearchDelegate?.dropThePin(placemark: selectedItem)
dismiss(animated: true, completion: nil)
}
}
Code in another view controller that displays search bar and activates search display
let locationSearchTable = storyboard!.instantiateViewController(withIdentifier: "CustomerAddSettingsLocationSearch") as! CustomerAddSettingsLocationSearch
resultSearchController = UISearchController(searchResultsController: locationSearchTable)
resultSearchController.searchResultsUpdater = locationSearchTable
let searchBar = resultSearchController!.searchBar
searchBar.sizeToFit()
searchBar.placeholder = "search custom address..."
searchBar.setBackgroundImage(UIImage(), for: .any, barMetrics: .default)
//navigationItem.titleView = resultSearchController?.searchBar
resultSearchController.hidesNavigationBarDuringPresentation = false
resultSearchController.dimsBackgroundDuringPresentation = true
definesPresentationContext = true
tableView.tableHeaderView = resultSearchController.searchBar
解决方案
推荐阅读
- sql - Postgresql - 如何在列中的任何数组中查找指定字符串
- php - 查看数组内容时出现空白页
- proxy - Elastic Beanstalk Java Web 应用程序战争文件部署然后 URL 说代理错误
- java - Gradle - 如果项目仍然具有 SNAPSHOT 依赖项,则抛出异常
- wpf - 在具有多行的数据网格单元格中居中文本
- bash - 压缩在特定日期创建的文件
- mysql - mysql问题节点js可以插入
- bash - 从 shellscript 中选择了一个选项,或者没有一个选项
- java - Java 如何自动选中下面的复选框?
- css - 带有导航栏样式的 Asp.net 菜单