Swift-UISearchController结果显示搜索栏和TableView单元格之间的间距较大

很抱歉,图片过大,我很久没发布了。无论如何,我的UISearchController显示的结果在搜索栏和tableview单元之间创建了很大的间距。这是一个非常不一致的错误。显示搜索结果时,从现有空间中消除此间距的正确方法是什么?谢谢!

编辑:出现不一致之处的地方是,如果您先进行搜索,然后单击“取消”,然后再进行另一次搜索,则UITableViewCells会像没有质量间隔一样按需拥抱搜索栏。

Swift-UISearchController结果显示搜索栏和TableView单元格之间的间距较大

UISearchController显示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)
    }
    
}

另一个显示搜索栏并激活搜索显示的视图控制器中的代码

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
iCMS 回答:Swift-UISearchController结果显示搜索栏和TableView单元格之间的间距较大

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/1949556.html

大家都在问