1

Code:

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)
    }

}

I created a custom cell in a UITableView having a MKMapView. When I load map view using annotation for each row then my code is working fine. But the problem is when I scroll table view, map view is loading every time and scrolling is also not smooth enough. Let me know how can i fix this issue. I have share the code help me find out what is wrong in my code. Any help will be appreciated. Thanks in advance.

Ankit Jayaswal
  • 5,549
  • 3
  • 16
  • 36
Karthik
  • 11
  • 5
  • Please refer this link: https://stackoverflow.com/questions/34851795/how-to-get-static-image-from-google-maps-in-ios. – Khushbu May 24 '18 at 09:01
  • see this once it will helps you https://stackoverflow.com/questions/8420764/ios-notification-when-mkmapview-is-loaded-and-annotations-overlays-are-added: – Anbu.Karthik May 24 '18 at 09:09

2 Answers2

1

You can use MKMapSnapshotter to scroll table view smoothly.

See also Swift 3 Add custom annotation pin to MKMapSnapShotter snapshot .

Kosuke Ogawa
  • 7,383
  • 3
  • 31
  • 52
0

Because you are dequeueing your cell every time you scroll. Instead of this you can generate your map cell just one time and you can return the same object in your cellForRowAt method.

U. Benlice
  • 901
  • 9
  • 14