Ios 使用mapView创建tableViewCell

Ios 使用mapView创建tableViewCell,ios,swift,mkmapview,Ios,Swift,Mkmapview,我正在创建一个包含mapView的tableViewCell。但是,我似乎无法根据包含tableView的viewController中的位置添加注释。我首先在CellForRowatinex中设置cell.location=orgObject.coordinate,然后将以下代码添加到自定义单元格中。但是location变量始终返回nil。我想这是因为在调用cellforrowatinexpath之前调用了awakeForNib?我如何解决这个问题 import UIKit import Ma

我正在创建一个包含mapView的tableViewCell。但是,我似乎无法根据包含tableView的viewController中的位置添加注释。我首先在CellForRowatinex中设置
cell.location=orgObject.coordinate
,然后将以下代码添加到自定义单元格中。但是
location
变量始终返回nil。我想这是因为在调用
cellforrowatinexpath
之前调用了
awakeForNib
?我如何解决这个问题

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
  }

}
从cellForRowAtIndexPath调用cell.showLocation(位置)

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)
}
从cellForRowAtIndexPath调用cell.showLocation(位置)

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)
}

mapView将在cell类中声明,并在awakefromNib中设置。因此,在showLocation方法中,您可以访问self.mapView。你们仍然面临这个问题吗?请提供详细信息Naah刚刚将单元格传递到showLocation以及不要将单元格作为showLocationMethod中的参数传递。您可以像self一样在Cell类中访问mapView。mapView将在Cell类中声明,并在awakefromNib中设置。因此,在showLocation方法中,您可以访问self.mapView。你们仍然面临这个问题吗?请提供详细信息Naah刚刚将单元格传递到showLocation以及不要将单元格作为showLocationMethod中的参数传递。您可以在单元格类中访问mapView,如self.mapView