Map App中的[[UITableView _configureCellForDisplay:forIndexPath:]中的TableViewController断言失败

时间:2019-04-21 20:22:46

标签: swift uitableview

我正在尝试创建一个允许用户搜索本地区域的地图应用程序。我遇到了TableViewController的问题,它无法识别单元格。我已将单元格配置为LocationCell类型,并为其指定了标识符“ cell”。我不知道该怎么办,也无法弄清楚我的错误在哪里。

class LocationSearchTableViewController: UITableViewController, UISearchResultsUpdating {
    var matchingItems:[MKMapItem] = [MKMapItem]() 
    var mapView: MKMapView? = nil

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return matchingItems.count
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath : IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! LocationCell
        let selectedItem = matchingItems[indexPath.row].placemark

        cell.titleLabel.text = selectedItem.name
        cell.subtitleLabel.text = parseAddress(selectedItem: selectedItem)
        return cell
    }

    func updateSearchResults(for searchController: UISearchController) {
        matchingItems.removeAll()
        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(completionHandler: {(response, error) in
            if let results = response {
                if let err = error {
                    print("Error occurred in search: \(err.localizedDescription)")
                } else if results.mapItems.count == 0 {
                    print("No matches found")
                } else {
                    print("Matches found")

                    for item in results.mapItems {
                        print("Name = \(item.name ?? "No match")")
                        print("Phone = \(item.phoneNumber ?? "No Match")")

                        self.matchingItems.append(item as MKMapItem)
                        print("Matching items = \(self.matchingItems.count)")

                        let annotation = MKPointAnnotation()
                        annotation.coordinate = item.placemark.coordinate
                        annotation.title = item.name
                        self.mapView!.addAnnotation(annotation)
                    }
                    self.tableView.reloadData()
                }
            }
        })
    }
}

0 个答案:

没有答案