UItableViewCell 更新错误的单元格标签文本
UItableViewCell updating wrong cell label text
我有一个 UITableView,我在其中通过 latlng 从 Geocoder 加载地址。
当我第一次向下滚动 tableview 时,一切都很好并且工作正常。
但问题是,当我向上滚动时,所有地址都丢失了它们的单元格。我是说
第 5 个单元格的地址现在显示在第 1 个单元格上。
这是我的 cellForRowAt tableview 方法
let cell = self.mytableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! HomeCell
let position = indexPath.row
let data = mVehicleList[position]
getAddress(lat: data.latitude.toD(), lng: data.longitude.toD(), text: cell.lbAddress)
// getAddress is extenstion of ViewCOnroller which is give addres of latlng
这是我的 getAddress(lat,lng,label) 扩展
extension UIViewController {
func getAddress(lat:Double,lng :Double, text : UILabel)
{
let location = CLLocation(latitude: lat, longitude: lng)
CLGeocoder().reverseGeocodeLocation(location, completionHandler: {(placemarks, error) -> Void in
if(placemarks != nil){
if placemarks!.count > 0 {
let pm = placemarks![0]
if(pm.subLocality != nil && pm.subAdministrativeArea != nil)
{
text.text = pm.subLocality!+" "+pm.subAdministrativeArea!
}else{
guard let addressDict = placemarks?[0].addressDictionary else {
return
}
if let formattedAddress = addressDict["FormattedAddressLines"] as? [String] {
text.text = formattedAddress.joined(separator: ", ")
}
}
}else{
text.text = "No address found"
}
}
}) } }
这是因为出队
if let addr = data.addressText {
cell.lbAddress.text = addr
}
else {
getAddress(indexPath.row,lat: data.latitude.toD(), lng: data.longitude.toD(), text: cell.lbAddress)
}
我建议对位置进行地理编码并使用检索到的地址更改模型,然后重新加载 table/indexPath,这将避免您在滚动 [=17= 时一次又一次地获得相同的地址] ,只要检查模型的位置,如果为 nil 则开始地理编码,如果没有则将其分配给标签
func getAddress(_ index:Int,lat:Double,lng :Double, text : UILabel) {
///
mVehicleList[index].addressText = formattedAddress.joined(separator: ", ")
// reload table/index
}
class model {
var state:State = .none
func geocode(){
gurad state == .none else { return }
state = .geocoding
CLGeocoder().reverseGeocodeLocation//// {
state = .geocoded // if success
}
}
}
enum State {
case geocoding,gecoded,none
}
我有一个 UITableView,我在其中通过 latlng 从 Geocoder 加载地址。 当我第一次向下滚动 tableview 时,一切都很好并且工作正常。 但问题是,当我向上滚动时,所有地址都丢失了它们的单元格。我是说 第 5 个单元格的地址现在显示在第 1 个单元格上。
这是我的 cellForRowAt tableview 方法
let cell = self.mytableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! HomeCell
let position = indexPath.row
let data = mVehicleList[position]
getAddress(lat: data.latitude.toD(), lng: data.longitude.toD(), text: cell.lbAddress)
// getAddress is extenstion of ViewCOnroller which is give addres of latlng
这是我的 getAddress(lat,lng,label) 扩展
extension UIViewController {
func getAddress(lat:Double,lng :Double, text : UILabel)
{
let location = CLLocation(latitude: lat, longitude: lng)
CLGeocoder().reverseGeocodeLocation(location, completionHandler: {(placemarks, error) -> Void in
if(placemarks != nil){
if placemarks!.count > 0 {
let pm = placemarks![0]
if(pm.subLocality != nil && pm.subAdministrativeArea != nil)
{
text.text = pm.subLocality!+" "+pm.subAdministrativeArea!
}else{
guard let addressDict = placemarks?[0].addressDictionary else {
return
}
if let formattedAddress = addressDict["FormattedAddressLines"] as? [String] {
text.text = formattedAddress.joined(separator: ", ")
}
}
}else{
text.text = "No address found"
}
}
}) } }
这是因为出队
if let addr = data.addressText {
cell.lbAddress.text = addr
}
else {
getAddress(indexPath.row,lat: data.latitude.toD(), lng: data.longitude.toD(), text: cell.lbAddress)
}
我建议对位置进行地理编码并使用检索到的地址更改模型,然后重新加载 table/indexPath,这将避免您在滚动 [=17= 时一次又一次地获得相同的地址] ,只要检查模型的位置,如果为 nil 则开始地理编码,如果没有则将其分配给标签
func getAddress(_ index:Int,lat:Double,lng :Double, text : UILabel) {
///
mVehicleList[index].addressText = formattedAddress.joined(separator: ", ")
// reload table/index
}
class model {
var state:State = .none
func geocode(){
gurad state == .none else { return }
state = .geocoding
CLGeocoder().reverseGeocodeLocation//// {
state = .geocoded // if success
}
}
}
enum State {
case geocoding,gecoded,none
}