我想在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 :(得分:0)
如果您将addressString
设为属性,则可以cellRowForAtIndexPath
访问该属性。
答案 1 :(得分:0)
您应该在-10 to +10
方法之上声明addressString
的属性。不在viewDidLoad
方法内。
在viewDidLoad
方法
viewDidLoad
然后在let addressString = String()
方法中更改此行
viewDidLoad
到
let addressString = (lines as! NSArray).componentsJoined(by: "\n")