UItableViewCell更新错误的单元格标签文本 [英] UItableViewCell updating wrong cell label text

查看:52
本文介绍了UItableViewCell更新错误的单元格标签文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UITableView,在这里我通过latlng从Geocoder加载地址. 当我第一次向下滚动tableview时,一切都很好&工作方式良好. 但是问题是当我向上滚动时,所有地址都丢失了它们的单元格.我是说 现在在第一个单元格上显示的第五个单元格的地址.

I have a UITableView, Where I'm loading address from Geocoder by latlng. when I scroll down tableview first time all is fine & working in good manner. But Problem is when I'm scroll up then all address lost their cell. I mean the address of 5th cell now showing on 1st cell.

这是我的cellForRowAt表视图方法

        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,这将避免您在滚动表时一次又一次地获得相同的地址,只需检查模型的位置是否nil然后启动地址解析,如果没有,则将其分配给标签

I suggest to geocode the location and alter the model with the retrieved address , then reload the table/indexPath , and that will save you from getting the same address again and again when you scroll the table , just check the model's location if nil then start the geocode , if not then assign it to the label

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
}

这篇关于UItableViewCell更新错误的单元格标签文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆