簡體   English   中英

Google在SWIFT 4中的UITableViewCell上出現內存管理問題

[英]Memory management issue google maps on UITableViewCell in SWIFT 4

我實現的方式是:

我有一個筆尖:UITableViewCell,並且有一個實現有兩個標記和兩個標記之間的路徑的谷歌地圖。

當我在UItableView上使用示例8個單元格運行該程序時, 由於內存管理問題而退出應用程序 ,它有時崩潰了

在筆尖文件“ 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的 tableViews上:

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 Maps。 Apple MapView可以顯示不同的內容(建築物,3D視圖,位置...)。如果您在Google Maps中有某種此類元素,請嘗試將其停用。

在我的應用中,正在使用的RAM從300更改為165。

停用MKMapView的元素

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM