EDITED QUESTION:
I’m trying to take annotations created in map view (with title/subtitle) and push all annotations on my map to a tableview showing the list with title/subtitle.
I have a RegionAnnotation.h/.m NSObject file that works the reverse geocoding I need to populate the pins on my MapViewController. This works just fine. I do a long press which create a pin and the title and subtitle show up and the reverse geocoding works.
Now I want to push the pin data to a tableview list. I have tried calling the region annotation information within the cellForRowAtIndexPath and I use UITableViewCellStyleSubtitle in order to get the correct format for the cell to populate the title and subtitle. However when I call the following:
if (cell == nil){
NSLog(@"if cell");
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
CLPlacemark *pins = [self.annotations objectAtIndex:indexPath.row];
RegionAnnotation *annotation = [[RegionAnnotation alloc] initWithLocationIdentifier:(NSString *)pins];
cell.textLabel.text = annotation.title;
I only get the title, which is this case is “Location Reminder”, however, the subtitle information which is the address does not populate the table. All that shows is the text “subtitle”.
How do I populate the cell with both title and subtile information. I’ve been working on this for over a month and can’t seem to find a solution. Please help.
Some comments on the latest code posted:
This doesn’t look right. The
pinsvariable is declared as aCLPlacemark(which is suspicious in itself becauseCLPlacemarkdoesn’t conform toMKAnnotationso why is it in an “annotations” array?) and then it is being casted anNSString *which has no relationship to aCLPlacemark. The cast is not going convertpinsinto a string — it’s going to treat the data pointed to bypinsas if it was aNSString(which it isn’t).There are too many other issues, questions and unknowns with the previous code that was posted as well.
Instead, I’ll give an example of how you might pass the annotations that are on the map view to a table view and show the data in the cells…
In the
PinListViewController(the one with the table view), we declare anNSArrayproperty to receive and reference the annotations:Next, in the
MapViewController, in the place where you want to present/push/show thePinListViewController, the code would be something like this:An important point here is that this example sends the entire annotations array. If you are showing the user’s current location using
showsUserLocation = YESthen the array will include that annotation as well. If you only want to send certain annotations, you’ll have to first build a new array containing the ones you want from the map view array and setplvc.annotationsequal to that new array. A simple way to do that is to loop through themapView.annotationsand if an annotation is one you want to include, add it to the new array usingaddObject. Another possible issue with using the map view’sannotationsarray directly is that if the annotations on the map change (are added/removed) while the table view is still showing the annotations list, it will go out of sync and may cause run-time range exceptions. To avoid that, if necessary, you could setplvc.annotationsto a copy of the map view’s annotations array (ie.[mapView.annotations copy]).In
PinListViewController, in thenumberOfRowsInSectionmethod:In
PinListViewController, in thecellForRowAtIndexPathmethod:The
annotationis declared asid<MKAnnotation>so it will work with any type of annotation since the example only needs to show the standardtitleandsubtitleproperties. If you needed to show custom properties you may have in your custom annotation class, you would useisKindOfClassto check ifannotationis of that type and then you can cast it to that custom class and reference the custom properties.