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)
}
您需要每次更新後重新加載表。 – rmaddy