2017-01-03 51 views
0

我想顯示當前位置地址TableViewCell。我得到了經度和緯度,但我不明白如何在單元格上顯示地址字符串。如何在iOS的tableViewCell上顯示當前位置

我在ViewDidLoad()中寫下面的代碼 它在標籤中顯示地址。

let manager: CLLocationManager = locationManager 
    CLGeocoder().reverseGeocodeLocation(manager.location!, completionHandler: {(placemarks, error)->Void in 

let placemark = placemarks?[0] 

let lines = placemark?.addressDictionary?["FormattedAddressLines"] 

let addressString = (lines as! NSArray).componentsJoined(by: "\n") 

self.lblAddress.text = addressString 

print(addressString) 

}) 


but i want to show the address on tableview Cell. 

//return cell for perticular row withing section 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
{ 
    if indexPath.section <= 1 
    { 

     let cell = tableView.dequeueReusableCell(withIdentifier: "CellRID", for: indexPath) 

     cell.textLabel?.text = "\(self.batteryLevel() * 100) %" 

     return cell 
    } 

    else 

    { 

      //create reusable cell object by dequeueReusableCellWithIdentifier 

      //identifier must register with cell 

     let cell = tableView.dequeueReusableCell(withIdentifier: "LocationTableViewCellRID", for: indexPath) as! LocationTableViewCell 

     let personName = txtFieldName.text 

     let a = LocationVC() 

     cell.lblName?.text = "\(personName) \(self.batteryLevel() * 100) %" 

     cell.lblAddress?.text = 

     return cell 
    } 
} 

我想在lblAddress上顯示地址。我是iOS新手。在此先感謝您的幫助。

+0

您能夠從LAT-LON獲得位置的地址?如果是,然後創建一個字符串(說addressString)對象,其中包含地址&cellforRowAtIndexPath cell.lblAddress?.text = addressString –

+0

我已經嘗試,但發現錯誤,「使用未解析的標識符'addressString'」 – Innate

+0

你在哪裏聲明你的字符串? –

回答

0

您應該在viewDidLoad方法上聲明addressString的屬性。不在viewDidLoad方法中。

viewDidLoad方法改變這一行

let addressString = (lines as! NSArray).componentsJoined(by: "\n") 

添加這上面的viewDidLoad方法

let addressString = String() 

然後

self.addressString = (lines as! NSArray).componentsJoined(by: "\n") 
+0

謝謝。有用 – Innate

0

如果您使addressString屬性,您將有權訪問它在cellRowForAtIndexPath

+0

我已經嘗試,但發現錯誤,「使用未解析的標識符'addressString'」 – Innate

相關問題