内存管理问题SWIFT 4中UITableViewCell上的谷歌地图



我实现的方式是:

我有一个笔尖:UITableViewCell,有一个用两个标记和两个标记之间的路径实现的谷歌地图。

当我在UItableView上运行带有示例8个单元格的程序时,它有时会崩溃,原因是内存管理问题导致应用程序退出

在nib文件中"BookingHistoryTableViewCell">

import UIKit
import Google
import GoogleMaps
import GoogleMapsCore
import GooglePlaces
import GoogleToolboxForMac
class BookingHistoryTableViewCell: UITableViewCell {
//MARK: Outlets
@IBOutlet weak var bottomView: UIView!
@IBOutlet weak var mapView: GMSMapView!
override func awakeFromNib() {
super.awakeFromNib()
self.bottomView.layer.shadowColor = UIColor(displayP3Red: 0, green: 0, blue: 0, alpha: 1).cgColor
self.bottomView.layer.shadowRadius = 3
self.bottomView.layer.shadowOpacity = 1
self.bottomView.layer.shadowOffset = CGSize(width: 0, height: 0)
}
//main gate : 6.795046, 79.900768
func setupMap(){
let lat = 6.797222
let lon = 79.902028
let camera = GMSCameraPosition.camera(withLatitude: lat, longitude: lon, zoom: 17.0)
let googleMapView = GMSMapView.map(withFrame: self.mapView.bounds, camera: camera)
//        googleMapView.isMyLocationEnabled = true
//        googleMapView.settings.tiltGestures = false
//        googleMapView.settings.zoomGestures = false
//        googleMapView.settings.scrollGestures = false
//        googleMapView.accessibilityScroll(UIAccessibilityScrollDirection.left)
//        googleMapView.mapStyle(withFilename: "googleMapsStyle02", andType: "json")
self.mapView.addSubview(googleMapView)
//(googleMapView, at: 0)
let marker = GMSMarker()
marker.position = CLLocationCoordinate2DMake(lat, lon)
marker.title = "Main Title"
marker.snippet = "Deescription"
marker.map = googleMapView
}
override func layoutSubviews() {
super.layoutSubviews()
//self.setupMap()
}
override func layoutIfNeeded() {
super.layoutIfNeeded()
self.setupMap()
}
override func setNeedsLayout() {
super.setNeedsLayout()
//        self.setupMap()
}

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

然后在MyBookingsViewController的表视图上:

viewDidLoad(){
self.tableView.register(UINib(nibName: "BookingHistoryTableViewCell" , bundle: nil), forCellReuseIdentifier: "BookingHistoryTableViewCell")
self.tableView.rowHeight = UITableViewAutomaticDimension
self.tableView.estimatedRowHeight = 30
}

然后作为扩展,我有表控件:

extension MyBookingsViewController: UITableViewDelegate, UITableViewDataSource{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 8
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "BookingHistoryTableViewCell", for: indexPath) as! BookingHistoryTableViewCell
cell.selectionStyle = .none
return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("selected row number : (indexPath.row)")
}
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let editAction = UIContextualAction(style: .normal, title: "Edit") { (action, view, handler) in
print("tapped on Edit : (indexPath.row)")
}
let deleteAction = UIContextualAction(style: .destructive, title: "Delete") { (action, view, handler) in
print("tapped on Delete : (indexPath.row)")
}
editAction.backgroundColor = UIColor(displayP3Red: 183/255, green: 183/255, blue: 183/255, alpha: 0.9)
editAction.image = #imageLiteral(resourceName: "icon_edit_white")
deleteAction.backgroundColor = UIColor(displayP3Red: 251/255, green: 86/255, blue: 92/255, alpha: 0.9)
deleteAction.image = #imageLiteral(resourceName: "icon_delete_white")
let configuaration = UISwipeActionsConfiguration(actions: [deleteAction, editAction])
configuaration.performsFirstActionWithFullSwipe = true
return configuaration
}
}

这是有点需要的,在现实生活中,会有100个这样的细胞。有没有什么方法可以只将可见单元格渲染到显示区域,或者有什么方法可以节省内存?

我的建议是不要实现每个单元格的映射。我也遇到了同样的麻烦,但不同的是我使用的是苹果地图。Apple MapView允许显示不同的东西(建筑物、3D视图、位置…(如果你在谷歌地图中有这种元素,请尝试停用它。

在我的应用程序中,使用RAM从300变为165。

要停用MKMapView 的元素

最新更新