如果表为空,则显示地图

时间:2017-12-25 14:49:11

标签: swift xcode google-maps uitableview swift4

这是我试图在UITable为空时显示地图的代码

var mapView:GMSMapView?

     func numberOfSections(in tableView: UITableView) -> Int {
        if orders.count > 0 {
            self.tableview.backgroundView = nil
            self.tableview.separatorStyle = .singleLine
            return 1
        }

        mapView = GMSMapView.map(withFrame: CGRect(x: 100, y: 100, width: 200, height: 200), camera: GMSCameraPosition.camera(withLatitude: 51.050657, longitude: 10.649514, zoom: 5.5))


        //so the mapView is of width 200, height 200 and its center is same as center of the self.view
        mapView?.center = self.view.center
       self.tableview.backgroundView = mapView


        return 0
    }

但是项目崩溃了,原因是

  

libc ++ abi.dylib:以NSException类型的未捕获异常终止

2 个答案:

答案 0 :(得分:0)

问题在于,无论以前的情况如何,你都会返回0

func numberOfSections(in tableView: UITableView) -> Int {

    if orders.count > 0 {

        tableview.backgroundView = nil
        tableview.separatorStyle = .singleLine
        return 1

    } else {

        mapView = GMSMapView.map(withFrame: CGRect(x: 100, y: 100, width: 200, height: 200), camera: GMSCameraPosition.camera(withLatitude: 51.050657, longitude: 10.649514, zoom: 5.5))
        mapView?.center = view.center
        tableview.backgroundView = mapView
        return 0

    }

}

答案 1 :(得分:0)

好像你没有在AppDelegate中初始化APIkey。只需按照此步骤操作即可。

在AppDelegate中导入此文件。

import GoogleMaps 
import GooglePlaces

然后在didFinishLaunchingWithOptions方法中添加api密钥。

GMSPlacesClient.provideAPIKey("Your key")
GMSServices.provideAPIKey("Your key")

注册App时,您可以从Google Developers Console获取API密钥。

有关详细信息,请查看以下链接: - Developers Console

相关问题