0

我有一个 Viewcontroller,里面有一个 tableview,它创建一个自定义单元格,其中包含来自我的 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
}

}

这是带有 tableview 和信息的 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 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.
}
4

1 回答 1

0

didSelectRowAt您使用的属性currentLocation中,它是由 提供的最后一个单元格的位置cellForRowAt:。您需要做的是访问BusinessInfo数组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)

}

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

于 2017-06-13T00:56:41.863 回答