Reputation: 11193
I'm creating a tableViewCell with a mapView inside. however i can't seem to add a annotation based on a location in the viewController containing the tableView. i've started by in the cellForRowAtIndex by setting cell.location = orgObject.coordinate
after that i've added following code to my custom cell. However location
variable keep returning nil. i guess this is because awakeForNib
is called before cellForRowAtIndexPath
is called? how do i solve this?
import UIKit
import MapKit
class MapTableViewCell: UITableViewCell, MKMapViewDelegate {
var location: CLLocation?
@IBOutlet var mapView: MKMapView?
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
//MapView
mapView!.showsPointsOfInterest = true
if let mapView = self.mapView
{
mapView.delegate = self
}
let orgLocation = CLLocationCoordinate2DMake(location!.coordinate.latitude, location!.coordinate.longitude)
let dropPin = MKPointAnnotation()
dropPin.coordinate = orgLocation
mapView!.addAnnotation(dropPin)
self.mapView?.setRegion(MKCoordinateRegionMakeWithDistance(orgLocation, 500, 500), animated: true)
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
Upvotes: 0
Views: 2207
Reputation: 283
override func awakeFromNib() {
super.awakeFromNib()
// Initialisation code
//MapView
mapView!.showsPointsOfInterest = true
if let mapView = self.mapView
{
mapView.delegate = self
}
}
Call cell.showLocation(location) from cellForRowAtIndexPath
func showLocation(location:CLLocation) {
let orgLocation = CLLocationCoordinate2DMake(location!.coordinate.latitude, location!.coordinate.longitude)
let dropPin = MKPointAnnotation()
dropPin.coordinate = orgLocation
mapView!.addAnnotation(dropPin)
self.mapView?.setRegion(MKCoordinateRegionMakeWithDistance(orgLocation, 500, 500), animated: true)
}
Upvotes: 2