iosuitableviewswift3cllocationcoordinate2d

Custom Cell sending one coordinate to all locations,


I have A Viewcontroller with a tableview inside, which creates a custom cell containing information from my json and when the cell is taped, it sendS information to another view which shows the business location in a map, and the location name on the annotation. My problem is the cell sendS the information but on the map it shows the same latitude and longitude coordinates for every cell tapped and I can't seem to understand why.

Here is what I have:

this is my custom cell view controller:

import UIKit

class BusinessImgTableViewCell: UITableViewCell {

@IBOutlet weak var businessImg:     UIImageView!

@IBOutlet weak var businessNameLbl: UILabel!
@IBOutlet weak var distanceLbl:     UILabel!


override func awakeFromNib() {
    super.awakeFromNib()



    let view = UIView(frame: businessImg.frame)
    let gradient = CAGradientLayer()
    let arrayColors:Array<AnyObject> = [UIColor.clear.cgColor, UIColor.black.cgColor]

    gradient.frame = view.frame
    gradient.locations = [0.0, 1.0]
    gradient.colors = arrayColors


    view.layer.insertSublayer(gradient, at: 0)
    businessImg.addSubview(view)
    businessImg.bringSubview(toFront: view)
    // Initialization code
}

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

    // Configure the view for the selected state
}

}

this is the viewContoller with the tableview and information[won't add json because I know the problem is not there]:

class SearchViewController: UIViewController, CLLocationManagerDelegate, UITableViewDelegate, UITableViewDataSource{


@IBOutlet weak var businessTableView: UITableView!
@IBOutlet weak var menuBTN:           UIBarButtonItem!

               var businessName:      String!
               var currentLocation:   CLLocationCoordinate2D?




var BusinessInfo = [Business]()    

var locationManager = CLLocationManager()


override func viewDidLoad() {
    super.viewDidLoad()


    self.locationManager.requestAlwaysAuthorization()
    self.locationManager.requestWhenInUseAuthorization()

    if CLLocationManager.locationServicesEnabled() {

        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.requestLocation()



    }

    businessTableView.delegate = self
    businessTableView.dataSource = self

    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}



struct Business {

    let name: String
    let image: NSURL
    let city: String
    let distance: String
    let businessLoc: CLLocationCoordinate2D

}


func locationManager(_ manager: CLLocationManager, didFailWithError error: Error){

    // Handle errors here
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { // #warning Incomplete implementation, return the number of rows return BusinessInfo.count }

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! BusinessImgTableViewCell

    let businessInformation = BusinessInfo[indexPath.row]
    let imgdirectory = businessInformation.image

    let urlString: URL = imgdirectory as URL
    let data = try? Data(contentsOf: urlString)

    let myDistance = Double(businessInformation.distance)

        cell.businessImg.image = UIImage(data: data!)
        cell.businessNameLbl.text = businessInformation.name
        cell.distanceLbl.text = String(format: "%.2f", myDistance!) + " mi."
        cell.distanceLbl.sizeToFit()
        cell.businessNameLbl.sizeToFit()
        currentLocation = businessInformation.businessLoc



    return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    let indexPath = tableView.indexPathForSelectedRow
    let currentCell = tableView.cellForRow(at: indexPath!) as! BusinessImgTableViewCell


    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let mydestination = storyboard.instantiateViewController(withIdentifier: "businessInfoView") as! BusinessInfoViewController

    mydestination.businessCoor = currentLocation
    mydestination.businessName = currentCell.businessNameLbl.text
    self.navigationController!.pushViewController(mydestination, animated: true)



}

and this is the view showing the map

class BusinessInfoViewController: UIViewController, MKMapViewDelegate {

@IBOutlet weak var businessLoc: MKMapView!
@IBOutlet weak var backBTN: UIBarButtonItem!

@IBOutlet weak var backbtn: UIButton!

var businessCoor: CLLocationCoordinate2D!

   var businessName: String!





override func viewDidLoad() {
    super.viewDidLoad()



    let initialLocation = CLLocationCoordinate2DMake(businessCoor.latitude, businessCoor.longitude)

    let span = MKCoordinateSpanMake(0.01, 0.01)
    let region = MKCoordinateRegionMake(initialLocation, span)

    businessLoc.setRegion(region, animated: true)

    let annotation = MKPointAnnotation()
    annotation.coordinate = initialLocation
    annotation.title = businessName

    businessLoc.addAnnotation(annotation)

    self.businessLoc.delegate = self


// Do any additional setup after loading the view.
}

Solution

  • In didSelectRowAt you are using the property currentLocation, which is the location for the last cell that was provided by cellForRowAt:. What you need to do is access the BusinessInfo array at indexPath.row

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    
        let indexPath = tableView.indexPathForSelectedRow
        let currentCell = tableView.cellForRow(at: indexPath!) as! BusinessImgTableViewCell
    
    
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let mydestination = storyboard.instantiateViewController(withIdentifier: "businessInfoView") as! BusinessInfoViewController
        let business = BusinessInfo[indexPath.row]
        mydestination.businessCoor = business.businessLoc
        mydestination.businessName = business.name
        self.navigationController!.pushViewController(mydestination, animated: true)
    
    }
    

    FYI your array should be businessInfo, not BusinessInfo - variables, constants and properties should start with a lower case letter, not an uppercase letter.