gpt4 book ai didi

ios - CollectionView 单元格内的 MapView 不显示折线 Swift 4

转载 作者:行者123 更新时间:2023-11-29 05:54:54 25 4
gpt4 key购买 nike

我有一个 CollectionView,它显示保存到 CoreData 中的所有跟踪路线。除了单元格内 map 上缺少多段线之外,一切正常。我认为这是因为我忘记导入 CoreLocation 并将其委托(delegate)分配给 ViewCOntroller,但添加后它仍然没有显示。在cell配置中,我从获取的对象中打印数组,并且它打印正确。在我的主MapViewController中,我使用相同的过程,但这里没有按预期工作。你能看到我错过了什么吗?这是 ViewController` 的代码:



import UIKit
import MapKit
import CoreData
import CoreLocation

class MyRoutesViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, MKMapViewDelegate, CLLocationManagerDelegate {

@IBOutlet weak var routeCollectionView: UICollectionView!

@IBOutlet weak var myRoutesButton: UIButton!

var fetchedResultController: NSFetchedResultsController<Route>!


override func viewDidLoad() {
super.viewDidLoad()

routeCollectionView.dataSource = self
routeCollectionView.delegate = self

myRoutesButton.layer.borderWidth = 1
myRoutesButton.layer.masksToBounds = true
myRoutesButton.layer.cornerRadius = myRoutesButton.frame.height/4
myRoutesButton.layer.borderColor = UIColor.white.cgColor
configureFetchedResultsController()
}

func configureFetchedResultsController() {
let context = AppDelegate.viewContext
let fetchRequest = NSFetchRequest<Route>(entityName: "Route")
fetchRequest.sortDescriptors = [NSSortDescriptor(key: "name", ascending: true)]
fetchedResultController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: context, sectionNameKeyPath: nil, cacheName: nil)
do {
try fetchedResultController.performFetch()
print("fetched")
} catch {
fatalError("failed to fetch entities: \(error)")
}
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
guard let sections = self.fetchedResultController?.sections else {
fatalError("no sections in fetchedResultController" )
}
let sectionInfo = sections[section]
return sectionInfo.numberOfObjects
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Route Cell", for: indexPath as IndexPath) as! RouteCell
let route = self.fetchedResultController?.object(at: indexPath)

cell.titleValueLabel.text = route!.name //RoutesArray.routeArray[indexPath.row].routeName ?? ""
cell.distanceValueLabel.text = route!.distance //RoutesArray.routeArray[indexPath.row].distance ?? ""
cell.durationValueLabel.text = route!.duration //RoutesArray.routeArray[indexPath.row].duration ?? ""

do {
let decodedCoordinates = try JSONDecoder().decode([CLLocationCoordinate2D].self, from: route!.coordinates!)
print("decodedCoordinates are: \(decodedCoordinates)")
let geodesic = MKGeodesicPolyline(coordinates: decodedCoordinates, count: decodedCoordinates.count)
cell.map.add(geodesic)
cell.map.setRegion(MKCoordinateRegionForMapRect(geodesic.boundingMapRect), animated: true)
} catch {
print(error)
}
return cell
}

func collectionView(_ collectionView: UICollectionView, shouldShowMenuForItemAt indexPath: IndexPath) -> Bool {
return true
}


@IBAction func MyRoutesButton(_ sender: UIButton) {

performSegue(withIdentifier: "newRouteSegue", sender: self)
}


// RENDER OVERLAYS
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
if overlay is MKCircle {
let renderer = MKCircleRenderer(overlay: overlay)
renderer.fillColor = UIColor.black.withAlphaComponent(0.1)
renderer.strokeColor = UIColor.blue
renderer.lineWidth = 2
return renderer
} else if overlay is MKPolyline {
let renderer = MKPolylineRenderer(overlay: overlay)
renderer.strokeColor = UIColor.orange
renderer.lineWidth = 3
return renderer
} else if overlay is MKPolygon {
let renderer = MKPolygonRenderer(polygon: overlay as! MKPolygon)
renderer.fillColor = UIColor.black.withAlphaComponent(0.5)
renderer.strokeColor = UIColor.orange
renderer.lineWidth = 2
return renderer
}
return MKOverlayRenderer()
}
}

对于cell自定义类:

import UIKit
import MapKit
import CoreLocation

class RouteCell: UICollectionViewCell, MKMapViewDelegate {

@IBOutlet weak var map: MKMapView!
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var titleValueLabel: UILabel!
@IBOutlet weak var distanceLabel: UILabel!
@IBOutlet weak var distanceValueLabel: UILabel!
@IBOutlet weak var durationLabel: UILabel!
@IBOutlet weak var durationValueLabel: UILabel!

}

最佳答案

感谢@vadian 为我指明了正确的方向。我在定义 cell 属性时缺少将 cell 声明为 mapView delegate 。所以我刚刚在 cellForItemAt 函数中添加了 cell.map.delegate = self ,现在它可以正常工作了。再次感谢。

关于ios - CollectionView 单元格内的 MapView 不显示折线 Swift 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55261921/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com