代码:
TreeScreenVC:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let entityHarvest = arrHarvest[indexPath.row]
if entityHarvest.selfieDate != nil {
var cell: TreePlantScreenCell3 = (tableView.dequeueReusableCell(withIdentifier: "cell1") as! TreePlantScreenCell3?)!
let loc = CLLocationCoordinate2DMake(entityHarvest.digLat , entityHarvest.digLong )
let span = MKCoordinateSpanMake(1.02,1.02)
let region = MKCoordinateRegionMake(loc, span)
cell.mapView.setRegion(region, animated: true)
let lat = entityHarvest.digLat as Double
let long = entityHarvest.digLong as Double
let loc1 = CLLocation(latitude:lat, longitude:long)
cell.showLocation(location: loc1)
return cell
}
}
CustomCell:
class TreePlantScreenCell3: UITableViewCell,MKMapViewDelegate {
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
mapView!.showsPointsOfInterest = true
if let mapView = self.mapView {
mapView.delegate = self
}
}
//MARK: mapview delegate methods
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView?{
if (annotation is MKUserLocation) {
return nil
}
let reuseId = "test"
var anView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId)
if anView == nil {
anView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
anView?.frame.size = CGSize(width: 20.0, height: 21.0)
anView?.image = UIImage(named:"landmark.png")
anView?.canShowCallout = true
} else {
anView?.annotation = annotation
}
return anView
}
func showLocation(location:CLLocation) {
let orgLocation = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
let dropPin = MKPointAnnotation()
dropPin.coordinate = orgLocation
mapView!.addAnnotation(dropPin)
self.mapView?.setRegion(MKCoordinateRegionMakeWithDistance(orgLocation, 500, 500), animated: true)
}
}
我在具有MKMapView的UITableView中创建了一个自定义单元格。当我使用每行的注释加载地图视图时,我的代码工作正常。但问题是当我滚动表视图时,每次加载地图视图并且滚动也不够平滑。 让我知道如何解决这个问题。我分享了代码,帮助我找出代码中的错误。任何帮助将不胜感激。提前谢谢。
答案 0 :(得分:1)
答案 1 :(得分:0)
因为每次滚动都会使您的单元格出列。您可以只生成一次地图单元格,而不是这样,您可以在cellForRowAt方法中返回相同的对象。