菜鸟教程小白 发表于 2022-12-11 16:41:23

ios - Google Maps iOS SDK 未在 tableViewCell 中更新


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

<pre><code>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: ) {
      if let location = locations.first {
            mapView.camera = GMSCameraPosition(target: location.coordinate, zoom: 15, bearing: 0, viewingAngle: 0)
            locationManager.stopUpdatingLocation()

      }
    }
}
</code></pre>

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

<p>tableView中的代码</p>

<pre><code> override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -&gt; UITableViewCell {


            let reuseIdentifier = &#34;maps&#34;
            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
    }
</code></pre>

<p>我不知道我做错了什么</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我现在遇到了同样的问题,无法在 <code>UITableViewCell</code> 内放置任何 map 标记。</p>

<p>我发现,只有在单元格的 <code>awakeFromNib</code> 方法中调用它的方法时,Googlemap 才能在 <code>UITableViewCell</code> 中工作。 </p>

<p>我不知道为什么,但现在才发现。 </p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - Google Maps iOS SDK 未在 tableViewCell 中更新,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/37697355/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/37697355/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - Google Maps iOS SDK 未在 tableViewCell 中更新