将MapKit注释连接到表视图

问题描述 投票:2回答:1

我有视图控制器,其中下半部分是tableview,上半部分是地图。当它加载时,我调用API来获取附近的位置并将这些位置加载到数组中。加载数组后,我循环遍历数组,为每个位置的lat,lon和name设置一个注释,同一个数组是表视图的数据。我想要做的是连接表格单元格和注释。

当有人点击tableview时,我希望通过调出来选择相关的注释以进行选择。当有人点击注释时,我希望tableview在表视图中显示相关的单元格并突出显示它。我研究并发现mapView函数didSelect用于处理选定的注释,但我不知道如何(或者如果可以)连接2.现在我把它放在注释的副标题中,这是假的我从那个位置阅读所有细节,但这似乎是错误的方法。

...// load places into an array
            curBlipPlace.name = yelp.name
            curBlipPlace.distance = yelp.distance
            curBlipPlace.address1 = yelp.address1
            curBlipPlace.lat = yelp.lat
            curBlipPlace.lon = yelp.lon
            curBlipPlace.yelpId = yelp.yelpId
            curBlipPlace.yelp = yelp
            curBlipPlace.yelpArrayPosition = yelp.arrayPosition
            self.blipPlaces.append(curBlipPlace)

..// After filling array, sort it, loop to load annotations, and load to table
            self.blipPlaces.sort { $0.distance ?? 9999 < $1.distance ?? 9999 }
            for i in 0..<self.blipPlaces.count {
                if let lat = self.blipPlaces[i].lat, let lon = self.blipPlaces[i].lon {
                    let coordinate = CLLocationCoordinate2D(latitude: lat, longitude: lon)
                    let annotation = MKPointAnnotation()
                    annotation.coordinate = coordinate
                    annotation.title = self.blipPlaces[i].name
                    annotation.subtitle = String(i)

                    self.map.addAnnotation(annotation)
                }
            }

            self.PlaceTableView.reloadData()

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = PlaceTableView.dequeueReusableCell(withIdentifier: "fileCell", for: indexPath) as! BlipFileTVCell
    cell.placeLabel.text = "\(indexPath.row): \(blipPlaces[indexPath.row].name) - \(blipPlaces[indexPath.row].distance ?? 999)"
    return cell
}

..// Rube Goldberg way of connecting table cell and annotation by forcing array position into subTitle
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
    print("CLICKED: \(String(describing: view.annotation?.title))")
    if let tempString = (view.annotation?.subtitle) as? String {
        print("ok... \(tempString)")
        if let tempInt = Int(tempString) {
            print(self.blipPlaces[tempInt].name)
        }
    }
}

有没有更好的方法来连接注释和表格单元格?有没有办法让单元格点击激活注释?

此外,由于tapped注释可能是teh表中的最后一个单元格,如果它的屏幕外,有没有办法“移动”表格单元格?如果有人为列表中的最后一项选择注释,有没有办法让表格单元格“向上滚动”,以便一个可以查看而不是屏幕外?

-但

ios swift annotations tableview mapkit
1个回答
1
投票

实现自定义注释。

class CustomAnnotation: MKPointAnnotation {
    var index: Int!
}

将自定义注释添加到mapView。

let annotation = CustomAnnotation()
annotation.coordinate = coordinate
annotation.title = "title"
annotation.subtitle = "subtitle"
annotation.index = 0 // 0...n
self.mapView.addAnnotation(annotation)

你可以在index获得mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView)

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
    if let annotation = view.annotation as? CustomAnnotation {
        print(annotation.index)
        // Get the cell by index
        // You can use `cellForRowAtIndexPath:`
        // Then select the cell
    }
}

编辑:

// when the table view is clicked 
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    for annotation in mapView.annotations {
        if let annotation = annotation as? CustomAnnotation, annotation.index == indexPath.row {
            // select annotation and show callout
            mapView.selectAnnotation(annotation, animated: true)
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.