使用mapView创建tableViewCell

时间:2016-03-22 11:13:23

标签: ios swift mkmapview

我正在创建一个带有mapView的tableViewCell。但是我似乎无法根据包含tableView的viewController中的位置添加注释。我已经在cellForRowAtIndex中开始设置cell.location = orgObject.coordinate之后我已将以下代码添加到我的自定义单元格中。但是location变量保持返回零。我想这是因为在调用awakeForNib之前调用了cellForRowAtIndexPath?我该如何解决这个问题?

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
  }

}

1 个答案:

答案 0 :(得分:2)

Traceback (most recent call last):
  File "****.py", line 23, in <module>
    print file[1]['firstname']
AttributeError: DictReader instance has no attribute '__getitem__'

从cellForRowAtIndexPath

调用cell.showLocation(location)
override func awakeFromNib() {
      super.awakeFromNib()
      // Initialisation code

      //MapView
      mapView!.showsPointsOfInterest = true
      if let mapView = self.mapView
      {
          mapView.delegate = self
      }
}