【发布时间】:2017-09-03 20:56:17
【问题描述】:
我有一个表格视图,我想在 UITableViewCell 中显示一个自定义的 MKMarkerAnnotationView。这个 tableview 显示在一个专用的 ViewController 中,而 ViewController 在一个 TabBarController 中。第二个选项卡显示一个地图(但在这里并不重要)。
我创建了一个继承自 MKMarkerAnnotationView 的类“EdgePinViewAnnotation” 在我的故事板中,在 TableView 中,我添加了一个 UITableViewCell,其中包含一些标签和一个带有“EdgePinViewAnnotation”类的视图
在 UITableViewCell 的实现中,我正在初始化我的自定义“EdgePinViewAnnotation”。 不幸的是,当 Cell 显示在表格中时,显示的是默认的 MKMarkerAnnotationView,而不是我自定义的“EdgePinViewAnnotation”。
当我滚动 TableView 并且单元格离开屏幕然后刷新时,它会正确显示我的“EdgePinViewAnnotation”。
为什么第一次显示不正确?
这是我为自定义 MKMarkerAnnotationView 实现的代码
class EdgePinViewAnnotation: MKMarkerAnnotationView {
override var annotation: MKAnnotation? {
willSet {
if let edgeAnnotation = newValue as? EdgePin {
animatesWhenAdded = true
isDraggable = true
canShowCallout = true
initRenderingProperties(pin: edgeAnnotation)
}
}
}
func initWith(edgePin:EdgePin) {
animatesWhenAdded = false
isDraggable = false
canShowCallout = false
initRenderingProperties(pin: edgePin)
}
func initRenderingProperties(pin edgePin:EdgePin) {
glyphTintColor = UIColor.white
glyphText = "\(edgePin.index)"
markerTintColor = edgePin.renderingColor
}
}
这里是我的 UITableView 中的代码
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.section == 0 {
if let cell = tableView.dequeueReusableCell(withIdentifier: CellId.DistanceConfigurationCellId, for: indexPath) as? DistanceConfigurationTableViewCell {
cell.theLabel.text = findMe.edgePoints[indexPath.row].address
let coord = findMe.edgePoints[indexPath.row].coordinate
cell.coordinates.text = "Lat:\(coord.latitude) / Long:\(coord.longitude)"
cell.theTextField.text = "\(findMe.edgePoints[indexPath.row].distance)"
cell.theTextField.delegate = self
cell.theTextField.tag = indexPath.row
cell.theMarker.initWith(edgePin:findMe.edgePoints[indexPath.row])
return cell
}
} else {
return UITableViewCell()
}
}
class DistanceConfigurationTableViewCell: UITableViewCell {
@IBOutlet weak var theLabel: UILabel!
@IBOutlet weak var coordinates: UILabel!
@IBOutlet weak var theTextField: UITextField!
@IBOutlet weak var theMarker: EdgePinViewAnnotation!
}
【问题讨论】:
标签: ios swift uitableview mkannotationview