我是IOS编码新手,试图获取api天气数据并在tableview中显示,获得了数据但无法在tableview中显示。有谁能帮我解决这个问题吗?
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
}
}
问题是您没有将DataSource
分配给您的tableview
。
首先你需要替换
var models = [Name]()
var models: Name?
然后一旦你从服务器获得数据你需要将数据分配给models
即
self.models = try JSONDecoder().decode(Name.self, from: data)
你没有在这里提到你的Name
Codable,你可以参考下面的代码:
import Foundation
// MARK: - Name
struct Name: Codable {
let cod: String
let message, cnt: Int
let list: [List]
let city: City
}
// MARK: - City
struct City: Codable {
let id: Int
let name: String
let coord: Coord
let country: String
let population, timezone, sunrise, sunset: Int
}
// MARK: - Coord
struct Coord: Codable {
}
// MARK: - List
struct List: Codable {
let dt: Int
let main: MainClass
let weather: [Weather]
let clouds: Clouds
let wind: Wind
let visibility: Int
let pop: Double
let sys: Sys
let dtTxt: String
let rain: Rain?
enum CodingKeys: String, CodingKey {
case dt, main, weather, clouds, wind, visibility, pop, sys
case dtTxt = "dt_txt"
case rain
}
}
// MARK: - Clouds
struct Clouds: Codable {
let all: Int
}
// MARK: - MainClass
struct MainClass: Codable {
let temp, feelsLike, tempMin, tempMax: Double
let pressure, seaLevel, grndLevel, humidity: Int
let tempKf: Double
enum CodingKeys: String, CodingKey {
case temp
case feelsLike = "feels_like"
case tempMin = "temp_min"
case tempMax = "temp_max"
case pressure
case seaLevel = "sea_level"
case grndLevel = "grnd_level"
case humidity
case tempKf = "temp_kf"
}
}
// MARK: - Rain
struct Rain: Codable {
let the3H: Double
enum CodingKeys: String, CodingKey {
case the3H = "3h"
}
}
// MARK: - Sys
struct Sys: Codable {
let pod: Pod
}
enum Pod: String, Codable {
case d = "d"
case n = "n"
}
// MARK: - Weather
struct Weather: Codable {
let id: Int
let main: MainEnum
let weatherDescription: Description
let icon: Icon
enum CodingKeys: String, CodingKey {
case id, main
case weatherDescription = "description"
case icon
}
}
enum Icon: String, Codable {
case the03D = "03d"
case the04D = "04d"
case the04N = "04n"
case the10D = "10d"
case the10N = "10n"
}
enum MainEnum: String, Codable {
case clouds = "Clouds"
case rain = "Rain"
}
enum Description: String, Codable {
case brokenClouds = "broken clouds"
case lightRain = "light rain"
case moderateRain = "moderate rain"
case overcastClouds = "overcast clouds"
case scatteredClouds = "scattered clouds"
}
// MARK: - Wind
struct Wind: Codable {
let speed: Double
let deg: Int
}
在
的帮助下,可以很容易地从JSON
创建Codable
。https://app.quicktype.io
你的UITableViewDelegate
和UITableViewDataSource
方法看起来像:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return models?.list.count ?? 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = self.models?.list[indexPath.row].dtTxt ?? ""
return cell
}
更新后的代码将是:
import UIKit
import CoreLocation
class ViewController: UIViewController , CLLocationManagerDelegate, UITableViewDelegate, UITableViewDataSource {
@IBOutlet var tableview : UITableView!
var models: Name?
let locationManager = CLLocationManager()
var currentLocation : CLLocation?
override func viewDidLoad() {
super.viewDidLoad()
self.tableview.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
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 {
self.models = try JSONDecoder().decode(Name.self, from: data)
DispatchQueue.main.async {
self.tableview.reloadData()
}
}catch {
print(error.localizedDescription)
}
}.resume()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return models?.list.count ?? 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = self.models?.list[indexPath.row].dtTxt ?? ""
return cell
}
}
您还没有向模型的对象添加值。当天气API返回数据时,解析该数据并将解析后的数据保存到模型的对象中。
你正在使用一个模型对象来填充表视图中的数据。
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return models.count
}
请更新以下功能以达到您想要的结果。
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)
model = welcome
DispatchQueue.main.async {
self.tableview.reloadData()
}
} catch {
print(error.localizedDescription)
}
}.resume() }
在上面的代码片段中,model = welcome
是一个游戏规则改变者。