2017-09-03 53 views
0

我有一个tableview,我想在UITableViewCell中显示一个自定义的MKMarkerAnnotationView。这个tableview显示在一个专用的ViewController中,ViewController在TabBarController中。第二个标签显示一个地图(但这里并不重要)。自定义MKMarkerAnnotationView第一次在UITableViewCell中显示不正确

我创建了一个类“EdgePinViewAnnotation”从MKMarkerAnnotationView 继承了我的故事板,TableView中我添加一个UITableViewCell包含一些标签,并与类“EdgePinViewAnnotation”

在实施视图的UITableViewCell,我正在初始化我的自定义“EdgePinViewAnnotation”。 不幸的是,当单元显示在表中时,它是显示的默认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! 

} 

回答

0

我找到了解决办法,即使我真的不明白它的代码。

的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]) 
     } 
    } 
正确显示
相关问题