Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 尝试使用MKLocalSearch进行搜索,并使用UISearchController显示结果_Ios_Swift_Uitableview_Tableview - Fatal编程技术网

Ios 尝试使用MKLocalSearch进行搜索,并使用UISearchController显示结果

Ios 尝试使用MKLocalSearch进行搜索,并使用UISearchController显示结果,ios,swift,uitableview,tableview,Ios,Swift,Uitableview,Tableview,我是swift新手,我尝试创建一个应用程序来列出要访问的地方。我想使用MapKit在地图上包含搜索。即使它有点旧了,我也被人搀扶着 这是我的视图控制器“LocationSearchTable” 这与教程中的内容几乎相同。我只是更新扩展中的函数,并插入一些“print”来检查代码运行的部分 当我运行应用程序时,当我开始在搜索栏中键入任何字母时,会发生错误。错误是“UITableView数据源为索引路径{length=2,path=0-0}处的行返回了一个nil单元格 我在堆栈溢出上发现了一些具有相

我是swift新手,我尝试创建一个应用程序来列出要访问的地方。我想使用MapKit在地图上包含搜索。即使它有点旧了,我也被人搀扶着

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

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

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

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

我通过将Xcode中的Swift语言版本修改为5来解决这个问题。我真的不明白发生了什么

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