I am new to IOS coding tried to fetch api weather data and show it in tableview , got the data but unable to show it in Tableview. Could anyone help me on this issue.
import UIKit import CoreLocation
class ViewController: UIViewController , UITableViewDelegate , UITableViewDataSource , CLLocationManagerDelegate {
@IBOutlet var tableview : UITableView! var models = [Name]() let locationManager = CLLocationManager() var currentLocation : CLLocation? override func viewDidLoad() { super.viewDidLoad() tableview.register(TodayTableViewCell.nib(), forCellReuseIdentifier: TodayTableViewCell.identifier) tableview.register(WeatherlyTableViewCell.nib(), forCellReuseIdentifier: WeatherlyTableViewCell.identifier) tableview.delegate = self tableview.dataSource = self location() requestforuUpdatingLocation() } func location() { locationManager.delegate = self locationManager.requestWhenInUseAuthorization() locationManager.startUpdatingLocation() } func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { if !locations.isEmpty , currentLocation == nil { currentLocation = locations.first locationManager.stopUpdatingLocation() } } func requestforuUpdatingLocation() { let urlstring = "http://api.openweathermap.org/data/2.5/forecast?lat=0&lon=0&appid=2553332c1366762c77891d87b04c4325" let url = URL(string: urlstring)! URLSession.shared.dataTask(with: url) { data, response, error in guard let data=data , error == nil else{return} do { let welcome = try JSONDecoder().decode(Name.self, from: data) print(welcome) DispatchQueue.main.async { self.tableview.reloadData() } }catch { print(error.localizedDescription) } }.resume() } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return models.count }
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) cell.textLabel?.text = models[indexPath.row].cod return cell } }
https://stackoverflow.com/questions/66558237/not-showing-api-weather-data-in-the-tableview March 10, 2021 at 12:08PM
没有评论:
发表评论