自定义单元发送一个坐标到所有位置,

Custom Cell sending one coordinate to all locations,

我有一个 Viewcontroller,里面有一个表视图,它创建了一个自定义单元格,其中包含来自我的 json 的信息,当单元格被录音时,它将信息发送到另一个显示业务位置的视图在地图中,以及注释上的位置名称。我的问题是单元格发送了信息,但在地图上,它为每个点击的单元格显示了相同的纬度和经度坐标,我似乎不明白为什么。

这是我的资料:

这是我的自定义单元格视图控制器:

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
}

}

这是带有表视图和信息的 viewContoller[不会添加 json 因为我知道问题不在那里]:

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 不完整的实现,return 行数 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)



}

这是显示地图的视图

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

didSelectRowAt 中,您正在使用 属性 currentLocation,这是 cellForRowAt: 提供的最后一个单元格的位置。您需要做的是在 indexPath.row

处访问 BusinessInfo 数组
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)

}

仅供参考,您的数组应该是 businessInfo,而不是 BusinessInfo - 变量、常量和属性应该以小写字母开头,而不是大写字母。