2016-06-07 19 views
0

在自定義tableViewCell,我連接查看這是一個谷歌地圖類的自定義類如何在單元格中啓用Google地圖按鈕?

但是似乎這些代碼行不,我創建

此單元格的自定義單元格運行是的tableView的一部分,我想TableView中的viewDidLoad中實例化這個細胞例如var gmapsCell = GMapTableViewCell但似乎總是會返回nil

所以我我的方法更改爲下面的代碼

import UIKit 
import GoogleMaps 

class GMapTableViewCell: UITableViewCell, GMSMapViewDelegate { 

    let locationManager = CLLocationManager() 

    @IBOutlet var mapView: GMSMapView! 
    override func awakeFromNib() { 
     super.awakeFromNib() 
     locationManager.requestAlwaysAuthorization() 
     // For use in foreground 
     locationManager.requestWhenInUseAuthorization() 
     mapView.delegate = self 
     mapView.settings.myLocationButton = true 

    } 


    override func setSelected(selected: Bool, animated: Bool) { 
     super.setSelected(selected, animated: animated) 

     // Configure the view for the selected state 
    } 

} 


extension GMapTableViewCell: CLLocationManagerDelegate { 

    func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) { 

     if status == .AuthorizedWhenInUse { 
      locationManager.startUpdatingLocation() 
      mapView.myLocationEnabled = true 
      mapView.settings.myLocationButton = true 
     } 
    } 

    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 
     if let location = locations.first { 
      mapView.camera = GMSCameraPosition(target: location.coordinate, zoom: 15, bearing: 0, viewingAngle: 0) 
      locationManager.stopUpdatingLocation() 

     } 
    } 
} 

現在的問題是,

override func awakeFromNib() { 
     super.awakeFromNib() 
     locationManager.requestAlwaysAuthorization() 
     // For use in foreground 
     locationManager.requestWhenInUseAuthorization() 
     mapView.delegate = self 
     mapView.settings.myLocationButton = true 

    } 

這永遠不會運行。我的目標是簡單地到達設備的當前位置。有沒有自定義單元格的viewDidLoad的等價物?

回答

0

您需要在info.plist中爲位置管理器添加「NSLocationWhenInUseUsageDescription」鍵作爲字符串類型以開始更新位置。 enter image description here

+0

我已經很久以前加了這個。 – airsoftFreak

0

awakeNib()中的所有代碼都應該寫入tableView:cellForRowAtIndexPath中。 在那裏設置所有代表。 然後它會正常工作。

相關問題