我有一个ViewViewer里面有一个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
}
}
这是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部的:int) - >内部{ // #warning后不完全implemen塔季翁,返回的行数 返回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.
}
在'didSelect'中,您需要获取与所选行关联的位置。 'currentLocation'是否显示与最后一个单元格关联的位置 – Paulw11
您可以告诉我如何去做,我尝试了很多方法,似乎找不到解决方案,谢谢 – SCS
和'cellForRowAt'一样。使用'BusinessInfo [indexPath.row]' – Paulw11