0

So I was able to have the annotation's callout animate when the selected table cell was selected like so. Quite simple due to the indexPath.

  func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let indexPath = indexPath.row
    myMap.selectAnnotation(pinArray[indexPath] , animated: true)
}

However, I do not understand how to achieve this when I select the annotation and want the table cell to light up. I've been attempting to set a var to indexPath but because the annotation has no subscript, I am unable to perform this. Therefore, how would I be able to accomplish the annotation to selected cell logic?

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
    //Magic
}

Update - currently the code I have to highlight the table cell correlated to the map annotation is this.

  func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {

    let index = pinArray.index(of: view.annotation as! AnnotationPin)

    let indexPath = IndexPath(row: index!, section: 0)

    myTable.selectRow(at: indexPath, animated: true, scrollPosition: .top)

}

Unfortunately, I receive this error message when I click on a map annotation.

-[UITableView _contentOffsetForScrollingToRowAtIndexPath:atScrollPosition:usingPresentationValues:]: row (72) beyond bounds (17) for section (0).

I don't understand as the array only has 12 records tops, so I completely don't understand how a row above 15 can be mentioned to be beyond bounds.

Kosuke Ogawa
  • 7,383
  • 3
  • 31
  • 52

1 Answers1

0
  1. Get index of pinArray from selected annotation
  2. Make IndexPath
  3. Select row of TableView

e.g.

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
    // Get index of pinArray
    let index = pinArray.index{$0 === view.annotation}

    // Make IndexPath
    let indexPath = IndexPath(row: index, section: 0)

    // Select row of TableView
    tableView.selectRow(at: indexpath, animated: true, scrollPosition: .bottom)
}
Kosuke Ogawa
  • 7,383
  • 3
  • 31
  • 52
  • Almost perfect. However, depending on which annotation I select, the program will crash and xcode will say that the selected index is beyond bonds. which I don't understand. – Jonathan Gardocki Jun 30 '18 at 20:01
  • You should check if 'IndexPath' exists. See also https://stackoverflow.com/a/43618106/1155354 – Kosuke Ogawa Jul 01 '18 at 00:21