首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MapKit本地搜索结果填充表

MapKit本地搜索结果填充表
EN

Stack Overflow用户
提问于 2021-09-20 14:14:24
回答 1查看 71关注 0票数 0

我正在尝试加载更新的搜索结果,但它没有填充表视图。我使用了这个链接https://www.thorntech.com/how-to-search-for-location-using-apples-mapkit/,它属于以前的版本,但除了显示本地搜索结果之外,它仍然工作得很好。请帮帮忙

代码语言:javascript
复制
class LocationSearchTable : UITableViewController, UISearchResultsUpdating {
    var matchingItems:[MKMapItem] = []
    var mapView: MKMapView? = nil
    
}

extension LocationSearchTable {
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 {
            print("No response")
            return
        }
        self.matchingItems = response.mapItems
        self.tableView.reloadData()
    }
    }
}
extension LocationSearchTable {
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return matchingItems.count
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell")!
        let selectedItem = matchingItems[indexPath.row].placemark
        cell.textLabel?.text = selectedItem.name
        cell.detailTextLabel?.text = ""
        return cell
    }
}
EN

回答 1

Stack Overflow用户

发布于 2022-06-06 23:37:31

代码语言:javascript
复制
 //use IndexPath rather than NSIndexPath and you need to use 
 //override

override func tableView(_ tableView: UITableView, 
     cellForRowAtIndexPath 
     indexPath: IndexPath) -> UITableViewCell {
  let cell =tableView.dequeueReusableCell(withIdentifier:"cell")!
    let selectedItem = matchingItems[indexPath.row].placemark
    cell.textLabel?.text = selectedItem.name
    cell.detailTextLabel?.text = ""
    return cell
}

希望现在回答你还为时不晚!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69255949

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档