2017-09-03 51 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]) 
     } 
    } 
正確顯示
相關問題