2017-09-03 13 views
0

カスタムMKMarkerAnnotationViewにUITableViewCellで表示したいテーブルビューがあります。このテーブルビューは専用のViewControllerに表示され、ViewControllerはTabBarControllerにあります。 2番目のタブはマップを表示します(ただし、ここでは重要ではありません)。UITableViewCellでカスタムMKMarkerAnnotationViewが初めて正しく表示されない

私はいくつかのラベルと実装では、クラス「EdgePinViewAnnotation」

とビューが含まれているテーブルビューに、私はのUITableViewCellを追加した、私のストーリーボードでMKMarkerAnnotationView から継承するクラス「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! 

} 

答えて

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]) 
     } 
    } 
に正しく表示さに設定する必要があります
関連する問題