首页 > 解决方案 > 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.

enter image description here

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

标签: iosswiftuitableviewuisearchcontrolleruitableviewsectionheader

解决方案


推荐阅读