我想在MKAnnotationView中顯示幾個信息,但項目的數量並不總是相同的,所以我想在這個MKAnnotationView中創建一個不可滾動的UITableView。 我沒有設法做到這一點。當註釋被放置時,通過點擊它沒有註釋視圖。如何在MKAnnotationView中顯示UITableView?
用Joakim編輯評論: tableView顯示在AnnotationView外部,並且在外部點擊時不會消失。
extension MapViewController: MKMapViewDelegate {
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
let reuseIdentifier = "pin"
var pinView = self.mapView.dequeueReusableAnnotationView(withIdentifier: reuseIdentifier) as? MKPinAnnotationView
if pinView == nil {
pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseIdentifier)
let placesTableView = UITableView(frame: CGRect(x: 0, y: 0, width: 150, height: 200))
placesTableView.delegate = self
placesTableView.dataSource = self
placeData = ["France", "Ile de france", "Paris"]
pinView?.addSubview(placesTableView)
} else {
pinView?.annotation = annotation
}
return pinView
}
func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
GlobalVariables.sharedInstance.selectedCountry = mapView.selectedAnnotations[0].title!!
self.tabBarController?.selectedIndex = 0
FIRAnalytics.logEvent(withName: "location_from_map", parameters: [
"location": GlobalVariables.sharedInstance.selectedCountry as NSObject
])
}
}
extension MapViewController: UITableViewDelegate {
}
extension MapViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return placeData.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .default, reuseIdentifier: nil)
cell.textLabel?.text = placeData[indexPath.item]
return cell
}
}
PS:註釋觀點與其他代碼,我把所以我知道這個問題來自於實現代碼如下實施可見。
你的tableview似乎沒有任何大小或位置。那是對的嗎? – Starlord
謝謝Joakim,我編輯了這個問題,因爲它仍然沒有正確顯示。 – OxyFlax
也許我會嘗試'UIStackView' :-) – olejnjak