我是Swift Programming的新手。我正在尝试将Google地图置于UITableView
。我能这样做吗?
用户界面应如下所示:
以下是使用Storyboard实现UITableView的代码:
override func numberOfSections(in tableView: UITableView) -> Int {
return 3
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
var rowCount = 0
if section == 0 {
rowCount = 1
}
if section == 1 {
rowCount = arrayOfStatic.count
}
if section == 2 {
rowCount = arrayOfDynamic.count
}
return rowCount
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.section == 0 {
return 250
} else {
return 70
}
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.section == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "mapsCellId", for: indexPath) as! GoogleMapsCell
return cell
} else if indexPath.section == 1 {
let cell = tableView.dequeueReusableCell(withIdentifier: "staticCellId", for: indexPath) as! StaticCell
return cell
} else {
let cell = tableView.dequeueReusableCell(withIdentifier: "placesCellId", for: indexPath) as! PlacesCell
let ip = indexPath
cell.imageFoursquarePlaces.image = arrayOfDynamic[indexPath.row].image
cell.labelPlacesName.text = arrayOfDynamic[ip.row].name
cell.labelPlacesCategory.text = arrayOfDynamic[ip.row].category
return cell
}
}
以下是我在UITableViewCell
的第一部分中实施UITableView
的代码:
class GoogleMapsCell: UITableViewCell, GMSMapViewDelegate, CLLocationManagerDelegate {
@IBOutlet weak var googleMapsView: UIView!
@IBOutlet weak var buttonCurrentLoc: UIButton!
var googleMaps: GMSMapView!
var locationManager = CLLocationManager()
var camera = GMSCameraPosition()
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
self.showCurrentLocationOnMap()
self.locationManager.stopUpdatingLocation()
}
func showCurrentLocationOnMap() {
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.startUpdatingLocation()
let camera = GMSCameraPosition.camera(withLatitude: (self.locationManager.location?.coordinate.latitude)!, longitude: (self.locationManager.location?.coordinate.longitude)!, zoom: 15)
self.googleMaps = GMSMapView.map(withFrame: CGRect(x: 0,y: 0, width: self.googleMapsView.frame.size.width, height: self.googleMapsView.frame.height), camera: camera)
do {
if let styleURL = Bundle.main.url(forResource: "style", withExtension: "json") {
self.googleMaps.mapStyle = try GMSMapStyle(contentsOfFileURL: styleURL)
} else {
NSLog("Unable to find style.json")
}
} catch {
NSLog("The style definition could not be loaded: \(error)")
}
self.googleMaps.isMyLocationEnabled = true
self.googleMaps.accessibilityElementsHidden = false
self.addSubview(self.googleMaps)
self.googleMaps.camera = camera
self.addSubview(self.buttonCurrentLoc)
}
}
它给了我结果:
我有searchBar
作为HeaderView
。我试图通过在TableView的第一个单元格内创建UIView
来放置Google Map View。但我无法显示谷歌地图视图,无法点击该按钮。我怎么能这样做?
任何帮助将不胜感激:)
答案 0 :(得分:0)
您忘记在showCurrentLocationOnMap
中使用GoogleMapsCell
的实例调用cellForRowAtIndexPath
方法。
let cell = tableView.dequeueReusableCell(withIdentifier: "mapsCellId", for: indexPath) as! GoogleMapsCell
//add this method call
cell.showCurrentLocationOnMap()
return cell
或者您可以覆盖awakeFromNib
中的GoogleMapsCell
,然后使用showCurrentLocationOnMap
方法调用它。
override func awakeFromNib() {
super.awakeFromNib()
self.showCurrentLocationOnMap()
}