【问题标题】:Custom MKMarkerAnnotationView not displayed correctly in UITableViewCell the first time自定义 MKMarkerAnnotationView 第一次在 UITableViewCell 中没有正确显示
【发布时间】: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


    【解决方案1】:

    我找到了解决方案,即使我不是很明白。

    MKMarkerAnnotationView 必须配置在

    tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) 
    

    而不是

    tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    

    然后下面的代码就可以工作了,MKMarkerAnnotationView 在每个 UITableViewCell 中都正确显示了

    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
    
                    return cell
                }
            } else {
         return UITableViewCell()
        }
    }
    
      func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
            if let theCell = cell as? DistanceConfigurationTableViewCell {
    
                theCell.theMarker.initWith(edgePin:findMe.edgePoints[indexPath.row])
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2020-12-13
      • 1970-01-01
      • 2016-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多