2017-01-30 1 views
-1

私はアドレスを含むセルを持つテーブルビューを持っており、ユーザーはそのアドレス(UILabel)をタップし、Appleのマップアプリケーションのその場所に移動できるようにします。マップにUILabelを接続する

ロケーションは配列なので、どのセルをどのように解析して、各セルの各位置に(タップして)Appleマップ上のその位置に移動できるかわかりません。

UPDATE:コードが今働いている、解決策は以下の存在である:

import UIKit 
import MapKit 

class FirstEventCell: UITableViewCell { 
    @IBOutlet weak var eventLocation: UILabel! 

@IBAction func goToMaps(_ sender: UIButton) { 
    CLGeocoder().geocodeAddressString((eventLocation?.text)!, completionHandler: {(placemarks, error) in 

     if error != nil { 
      print("") 
     } else if placemarks!.count > 0 { 
      let placemark = placemarks![0] 
      let location = placemark.location 
      let coords = location!.coordinate 
      let coordinate = CLLocationCoordinate2DMake(coords.latitude,coords.longitude) 
      let mapItem = MKMapItem(placemark: MKPlacemark(coordinate: coordinate, addressDictionary:nil)) 
      mapItem.name = "Target location" 
      mapItem.openInMaps(launchOptions: [MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDriving]) 

     } 
    }) 
} 

} 


class EventsTableTableViewController: UITableViewController { 

var locations = ["Washington, DC 20565", "1600 Pennsylvania Ave NW, Washington, DC 20500", "East Capitol St NE & First St SE, Washington, DC 20004"] 

override func viewDidLoad() { 
    super.viewDidLoad() 
} 

override func numberOfSections(in tableView: UITableView) -> Int { 
    return 1 
} 

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return locations.count 
} 


override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "firstCell", for: indexPath) 

    let eventLocation = locations[indexPath.row] 

    if let eventCell = cell as? FirstEventCell { 
     eventCell.eventLocation.text = eventLocation 
    } 

    return cell 
} 
+0

didSelectRowAtIndexPathを定義する方法を知っていますか?私はこれがUITableViewに関する質問か、アドレスをMKMapViewのCLLocationに変換するかどうかはわかりません。 – dylanthelion

+0

@ dylanthelionは、唯一ではないにしても一般的なやり方です。複数のテーブルを持つ場合、それらは両方とも関数への呼び出しを生成するので、それらを区別することができなければなりません。 – curt

+0

@curt:おそらくこれは分かっていますが、同じビューで複数のテーブルを処理する正しい方法は、複数のUITableViewDelegatesとUITableViewDataSourcesを持つことです。 – dylanthelion

答えて

0

このコードは(3.0事前スウィフト)ヶ月で、更新する必要がプロジェクトの一部で、そうなら、私に教えてくださいそれは動作しません、私はあなたのためにそれを修正するために動作します。あなたが知る必要があるもの: 'self'はUIViewController、 'AddressTextField'はアドレスが入力されたUITextFieldで、(IBOutletを介して)ViewControllerのプロパティ、 'pm'はCLPLacemark、 'address 「CLLocationです:

CLGeocoder().geocodeAddressString(self.AddressTextField.text!, completionHandler: {(placemarks, error) -> Void in 
      if error != nil { 
       print("Reverse geocoder failed with error" + error!.localizedDescription) 
       return 
      } 

      if placemarks!.count > 0 { 
       let pm = placemarks![0] 
       let address = pm.location! 
      } 
     }) 

Stringとしてアドレスを取得するためにindexPath.rowを使用した後、あなたのdidSelectRowAtIndexPathに似た何かを置く、との代わりにそのアドレスを入れて 『self.AddressTextField.text』!。それがうまくいけば、 'address' CLLocation変数を使ってMKMapViewを構築することができます。

+0

それがうまくいって、更新されたコードが問題になっています! –

+0

素敵です!私は自分の答えを受け入れ、質問を終わらせて、あなたが投票をどのようにしているかを見ています。 – dylanthelion

+0

アドレスラベルの上に目に見えないボタンを作成してクリックしただけでは、セルのテキストをつかみ、その文字列を座標に変換して座標を使用して地図内の場所。それを行うには奇妙な方法ですが、驚くほど効果的です。 –

関連する問題