OStack程序员社区-中国程序员成长平台

标题: ios - Google Maps iOS SDK 未在 tableViewCell 中更新 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 16:41
标题: ios - Google Maps iOS SDK 未在 tableViewCell 中更新

我在普通 View Controller 上运行google maps ios SDK,它运行完美,它甚至显示当前位置但我决定放入一个tableViewCell,下面的代码将显示如何

import UIKit
import GoogleMaps

class GMapTableViewCell: UITableViewCell, GMSMapViewDelegate  {

    let locationManager = CLLocationManager()

    @IBOutlet var mapView: GMSMapView!
    override func awakeFromNib() {
        super.awakeFromNib()
    }


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

        }
    }
}

此代码所做的只是添加一个 google maps delegate 并扩展一个 CLLocationManagerDelegate。在扩展中,这是它应该更新设备当前位置的地方,它在正常的 UIView 中也能完美运行,但不是在这里。

tableView中的代码

 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {


            let reuseIdentifier = "maps"
            let cell = tableView.dequeueReusableCellWithIdentifier(reuseIdentifier, forIndexPath: indexPath) as! GMapTableViewCell
            // I inherit both GMSMapViewDelegate, CLLocationManagerDelegate, for tableView 
            cell.locationManager.delegate = self
            cell.locationManager.requestAlwaysAuthorization()
            cell.locationManager.requestWhenInUseAuthorization()
            cell.mapView.delegate = self
            cell.mapView.myLocationEnabled = true
            cell.mapView.settings.myLocationButton = true

            return cell
    }

我不知道我做错了什么



Best Answer-推荐答案


我现在遇到了同样的问题,无法在 UITableViewCell 内放置任何 map 标记。

我发现,只有在单元格的 awakeFromNib 方法中调用它的方法时,Google map 才能在 UITableViewCell 中工作。

我不知道为什么,但现在才发现。

关于ios - Google Maps iOS SDK 未在 tableViewCell 中更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37697355/






欢迎光临 OStack程序员社区-中国程序员成长平台 (http://ostack.cn/) Powered by Discuz! X3.4