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

を宣言しています。 ViewDidLoadの –

答えて

0

addressStringのプロパティをviewDidLoadメソッドの上に宣言する必要があります。メソッドviewDidLoadの中にはありません。

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

このライン viewDidLoad方法 viewDidLoad方法変更で次に

let addressString = String() 

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

これを追加

+0

ありがとうございました。できます – Innate

0

addressStringをプロパティにすると、cellRowForAtIndexPathにアクセスできます。

+0

私はすでにそれを試してみましたが、 "未解決の識別子 'addressString'の使用" – Innate

関連する問題