2017-07-17 58 views
0

我想從位置管理器函數中提取經度和緯度,然後呈現在UITableView中。我希望它能夠更新位置更新的TableView。但Collect陣列仍然是空的? 我該怎麼做? 任何輸入將不勝感激!使用位置管理器中的值在其他函數中的函數

這裏是我的代碼:

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代碼中使用Swift集合。爲什麼要將數字轉換爲集合中的字符串?這不合適。保留原始數據。只有在需要顯示數據時纔將數據轉換爲字符串。 – rmaddy

相關問題