2017-07-17 13 views
0

私は、ロケーションマネージャの関数から緯度と経度を抽出して、UITableViewに表示しようとしています。私はそれが場所の更新としてTableViewを更新したい。しかし、Collect配列はまだ空ですか? どうすればいいですか? すべての入力をいただければ幸いです!他の関数でLocation Manager関数の値を使用する

は、ここに私のコードです:

class ThirdViewController: UIViewController, CLLocationManagerDelegate, UITableViewDelegate, UITableViewDataSource{ 
    @IBOutlet weak var map: MKMapView! 
    @IBOutlet var Tableview: UIView! 

    let manager = CLLocationManager() 
    var Collect = [Double]() 

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 
     let location = locations[0] 
     let span:MKCoordinateSpan = MKCoordinateSpanMake(0.01,0.01) //shows the size of map screen 
     let myLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude,location.coordinate.longitude) 
     let region:MKCoordinateRegion = MKCoordinateRegionMake(myLocation, span) 
     map.setRegion(region, animated: true) 
     self.map.showsUserLocation = true 

     let lat = (location.coordinate.latitude) 
     let long = (location.coordinate.longitude) 
     Collect.append(lat) 
     Collect.append(long) 
    } 

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "TableCell") 
      cell.textLabel?.text = Collect[indexPath.row] 
     return cell 
    } 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     manager.requestWhenInUseAuthorization() 
     manager.startUpdatingLocation() 
     super.viewDidLoad() 
     print(Collect) 
    } 
+0

あなたがそれぞれ更新後のテーブルをリロードする必要があります。 – rmaddy

答えて

1
class ThirdViewController: UIViewController, CLLocationManagerDelegate, UITableViewDelegate, UITableViewDataSource{ 

@IBOutlet weak var map: MKMapView! 
@IBOutlet var Tableview: UIView! 

let manager = CLLocationManager() 
var Collect = NSMutableArray() 
var filterArr = NSArray() 

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 
    let location = locations[0] 
    let span:MKCoordinateSpan = MKCoordinateSpanMake(0.01,0.01) //shows the size of map screen 
    let myLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude,location.coordinate.longitude) 
    let region:MKCoordinateRegion = MKCoordinateRegionMake(myLocation, span) 
    map.setRegion(region, animated: true) 
    self.map.showsUserLocation = true 


    let lat = String(location.coordinate.latitude) 
    let long = String(location.coordinate.longitude) 
    Collect.append(lat) 
    Collect.append(long) 
    filterArr = Collect as NSMutableArray() 
    Tableview.reloadData() 


} 

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "TableCell") 
     cell.textLabel?.text = Collect[indexPath.row] 
    return cell 
} 




override func viewDidLoad() { 
    super.viewDidLoad() 
    manager.requestWhenInUseAuthorization() 
    manager.startUpdatingLocation() 
    super.viewDidLoad() 
    Tableview.delegate = self 
    Tableview.datasource = self 
    print(Collect) 
} 
+1

非スイフトアレイを使用するようにコードを変更したのはなぜですか?それは適切ではありません。 SwiftコードでSwiftコレクションを使用する。そして、コレクションの数字を文字列に変換するのはなぜですか?それは適切ではありません。生データを保存します。データを表示する必要がある場合のみ、データを文字列に変換します。 – rmaddy

関連する問題