【问题标题】:Trying to make a search with MKLocalSearch dans display results with UISearchController尝试使用 MKLocalSearch 进行搜索,但使用 UISearchController 显示结果
【发布时间】:2020-12-24 15:25:42
【问题描述】:

我是 swift 的新手,我尝试创建应用程序来列出要访问的地方。我想使用 MapKit 在地图上进行搜索。我被this tutorial 困住了,即使它有点老了。

这是我的视图控制器“LocationSearchTable”

import MapKit

class LocationSearchTable : UITableViewController {

    var matchingItems:[MKMapItem] = []
    var mapView: MKMapView? = nil
    
    
    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 LocationSearchTable : UISearchResultsUpdating {
    func updateSearchResults(for searchController: UISearchController) {
        print("patate")
        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
            print("++++ \(self.matchingItems.count) ++++")
            self.tableView.reloadData()
        }
    }
    

}

extension LocationSearchTable {
    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        print("\(self.matchingItems.count)  matchingitems")
        return matchingItems.count
    }

    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
    }
}

与教程中的几乎相同。我只是更新扩展中的函数,并插入一些“打印”来检查代码运行的部分。

当我运行应用程序时,当我开始在搜索栏中输入任何字母时出现错误。错误是“UITableView dataSource 为索引路径处的行返回了一个 nil 单元格: {length = 2, path = 0 - 0}。

我在堆栈溢出时发现了一些具有相同类型错误的消息,但我无法修复自己的错误。 委托和数据源在 Storyboard 中定义(使用表视图控制器时自动创建)。 matchingItems.countalways 返回 9 或 10,所以我对搜索有答案。 我认为展开单元格时会出现问题,我找不到解决该问题的代码。

【问题讨论】:

    标签: ios swift uitableview tableview


    【解决方案1】:

    我通过将 Xcode 中的 Swift Language Version 修改为 5 来解决这个问题。我不太明白发生了什么...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-05-01
      • 1970-01-01
      • 2019-05-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多