2017-01-03 59 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

相关问题