我有一个带有tableview的Viewcontroller,它创建一个包含来自我的json信息的自定义单元格,当单元格被录音时,它将信息发送到另一个视图,该视图显示地图中的商家位置,以及位置名称。注解。我的问题是单元格发送信息,但在地图上它显示了每个单元格的相同纬度和经度坐标,我似乎无法理解为什么。
这就是我所拥有的:
这是我的自定义单元格视图控制器:
chrome.system.network.getNetworkInterfaces(function (networkIfaceArray) {
for (var i = 0; i < networkIfaceArray.length; i++) {
var iface = networkIfaceArray[i];
if (iface.prefixLength < 32) {
addr_array = parseIP(iface.address);
mask = 32 - iface.prefixLength;
mask = Math.pow(2, mask) - 1;
pointer = 3;
while (mask > 255) {
addr_array[pointer++] = 255;
mask -= 255;
}
addr_array[pointer] = mask;
udp_sock.send(app.socketId, message,
addr_array.join("."), 5000, function (sendInfo) {
//check for response
});
}
}
});
这是带有tableview和信息的viewContoller [不会添加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
}
}
func tableView(_ tableView:UITableView,numberOfRowsInSection section:Int) - &gt; Int { // #warning不完整的实现,返回行数 返回BusinessInfo.count }
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, 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)
}
答案 0 :(得分: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)
}
FYI你的数组应该是businessInfo
,而不是BusinessInfo
- 变量,常量和属性应该以小写字母开头,而不是大写字母。