TableViewCelll可重用标识符LocationManager



我试图在TableViewCell内的标签中显示两个坐标之间的距离,但每次滚动时,标签的数据都会更改。。。我知道我需要使用可重用标识符,但我所尝试的一切都不起作用。。。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
let cell = tableView.dequeueReusableCell(withIdentifier: "JobCell", for: indexPath) as! JobTableViewCell
let job = jobs[indexPath.row]
cell.job = job
cell.categorie.text = job.categorie
cell.distance.text = JobTableViewCell.takenLocation
return cell
}

我计算TableViewCell类内部的距离

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let lastLocation = locations.last {
let geocoder = CLGeocoder()
//get job coordinates
geocoder.geocodeAddressString(job.location) { placemarks, error in
let placemarkW = placemarks?.first
if let placemark = placemarkW
{
let lat = placemark.location?.coordinate.latitude
let lon = placemark.location?.coordinate.longitude
let jobLocation = CLLocation(latitude: lat!, longitude: lon!)
//get user coordinates
let myLocation = CLLocation(latitude: lastLocation.coordinate.latitude, longitude: lastLocation.coordinate.longitude)
//get distance between coordinates
let distance = myLocation.distance(from: jobLocation) / 1000
self.distance.text = String(format: "%.01fkm", distance)
self.job.distance = distance
JobTableViewCell.takenLocation = String(format: "%.01km", distance)
} else {
self.distance.text = "Not Valid"
self.job.distance = 0.0

}
self.reloading?.reloadIt()
}
}
self.location.stopUpdatingLocation()
guard let _: CLLocationCoordinate2D = manager.location?.coordinate else { return }
}

我听说不重复使用Cells是一个非常糟糕的主意,那么有什么方法可以解决我的问题吗?(顺便说一句,分类标签有效(

编辑新的计算类别:

class CalculateDistance: NSObject, CLLocationManagerDelegate{
var reloading:Reloading?
let location = CLLocationManager()
var job: Job!
var noDistance: String = ""
static var gettingDistance: String = ""
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let lastLocation = locations.last {
let geocoder = CLGeocoder()
//get job coordinates
geocoder.geocodeAddressString(job.location) { placemarks, error in
let placemarkW = placemarks?.first
if let placemark = placemarkW
{
let lat = placemark.location?.coordinate.latitude
let lon = placemark.location?.coordinate.longitude
let jobLocation = CLLocation(latitude: lat!, longitude: lon!)
//get user coordinates
let myLocation = CLLocation(latitude: lastLocation.coordinate.latitude, longitude: lastLocation.coordinate.longitude)
//get distance between coordinates
let distance = myLocation.distance(from: jobLocation) / 1000
CalculateDistance.gettingDistance = String(format: "%.01fkm", distance)
} else {
self.noDistance = "Not Valid"
}
self.reloading?.reloadIt()
}
}
self.location.stopUpdatingLocation()
guard let _: CLLocationCoordinate2D = manager.location?.coordinate else { return }
}
}
  1. 创建一个单独的类来计算位置。Cell的工作是显示用户界面并与用户交互
  2. 在您的代码中,地理编码将在完成时为您提供占位符,它是异步工作的,这意味着将加载TableView单元格,但标记稍后会出现。计算距离,一旦得到,就调用tableView.reloadData(((不要忘记在主队列中调用它

如果您需要更多信息,请随时询问!

最新更新