ios - 为什么相同的代码不能在标签栏控制器的多个实例上运行?
问题描述
我正在构建一个天气应用程序,它将使用标签栏控制器来显示不同的天气数据集。每个标签栏视图都有一个表格视图,其中填充了来自我的网站的天气信息单元格。第一个视图(1/3)正常工作。所以我复制了代码,更改了命名约定并尝试创建第二个视图。但是,即使一切都相同,第二个视图也没有填充信息。
这是有效的部分:
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
// getTemperData()
// getWindData()
// getRainData()
@IBOutlet var table: UITableView!
var models = [TemperatureDataModel]()
override func viewDidLoad() {
super.viewDidLoad()
//Register cells
table.register(WeatherTableViewCell.nib(), forCellReuseIdentifier: WeatherTableViewCell.identifier)
table.delegate = self
table.dataSource = self
getTemperData()
}
override func viewDidAppear(_ animated: Bool){
super.viewDidAppear(animated)
}
//Tables
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return models.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: WeatherTableViewCell.identifier, for: indexPath) as! WeatherTableViewCell
cell.configure(with: models[indexPath.row])
return cell
}
func getTemperData(){
//Create the URLs
let temperatureDataUrl = URL(string: "https://weatherstationapi.azurewebsites.net/api/TemperatureSensor/GetData")
guard let requestURLTemp = temperatureDataUrl else {fatalError()}
//Create URL request
var request = URLRequest(url: requestURLTemp)
//Specifiy HTTP Method to use
request.httpMethod = "GET"
request.setValue("application/json", forHTTPHeaderField: "Accept")
request.setValue("Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1lIjoiaWxpci50YWlyaUB0dHUuZWR1IiwiaHR0cDovL3NjaGVtYXMueG1sc29hcC5vcmcvd3MvMjAwNS8wNS9pZGVudGl0eS9jbGFpbXMvbmFtZWlkZW50aWZpZXIiOiI4MjEzYzhhMy1iODgxLTQ4NmUtOGUyMC1mZmNlMDlmNGY0ZjgiLCJuYmYiOiIxNjA1MTE4NDA4IiwiZXhwIjoiMTYzNjY1NDQwOCJ9.HmLCUwnB5Esbao5NDmjflooHcdQK1BWjy1lk6LIKiJg", forHTTPHeaderField: "Authorization")
//Send HTTP Request
let task = URLSession.shared.dataTask(with: request){(data, response, error) in
guard let data = data else {print("did not get data")
return
}
do
{
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
let TemperatureData = try decoder.decode([TemperatureDataModel].self, from: data)
for singleValue in TemperatureData {
print(singleValue.temperature)
self.models.append(singleValue)
DispatchQueue.main.async {
self.table.reloadData()
}
}
} catch {
print("JSONDecoder error:", error)
}
};task.resume()
}
}
这部分基本相同但不起作用:
class TempAndHumidityController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet var windtable: UITableView!
var Windmodels = [WindDataModel]()
override func viewDidLoad() {
super.viewDidLoad()
//Register cells
windtable.register(WindTableViewCell.nib(), forCellReuseIdentifier: WindTableViewCell.identifier)
windtable.delegate = self
windtable.dataSource = self
getWindData()
}
override func viewDidAppear(_ animated: Bool){
super.viewDidAppear(animated)
}
//Tables
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return Windmodels.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let windcell = tableView.dequeueReusableCell(withIdentifier: WindTableViewCell.identifier, for: indexPath) as! WindTableViewCell
windcell.configure(with: Windmodels[indexPath.row])
return windcell
}
func getWindData(){
//Create the URLs
let WindDataUrl = URL(string: "https://weatherstationapi.azurewebsites.net/api/WindData/GetAllData")
//Create URL requests
guard let requestURLWind = WindDataUrl else {fatalError()}
var requestWind = URLRequest(url: requestURLWind)
//Request for Wind data
requestWind.httpMethod = "GET"
requestWind.setValue("application/json", forHTTPHeaderField: "Accept")
requestWind.setValue("Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1lIjoiaWxpci50YWlyaUB0dHUuZWR1IiwiaHR0cDovL3NjaGVtYXMueG1sc29hcC5vcmcvd3MvMjAwNS8wNS9pZGVudGl0eS9jbGFpbXMvbmFtZWlkZW50aWZpZXIiOiI4MjEzYzhhMy1iODgxLTQ4NmUtOGUyMC1mZmNlMDlmNGY0ZjgiLCJuYmYiOiIxNjA1MTE4NDA4IiwiZXhwIjoiMTYzNjY1NDQwOCJ9.HmLCUwnB5Esbao5NDmjflooHcdQK1BWjy1lk6LIKiJg", forHTTPHeaderField: "Authorization")
//Send HTTP Request
let task = URLSession.shared.dataTask(with: requestWind){(data, response, error) in
guard let data = data else {return}
do
{
let decoder = JSONDecoder()
// decoder.keyDecodingStrategy = .convertFromSnakeCase
let WindData = try decoder.decode([WindDataModel].self, from: data)
for singleValue in WindData {
print("Wind Speed: \(singleValue.windSpeed_MPH)")
print("Wind Direction: \(singleValue.windDirection)")
self.Windmodels.append(singleValue)
DispatchQueue.main.async {
self.windtable.reloadData()
}
}
//print("Temperature Data:", TemperatureData)
} catch {
print("JSONDecoder error:", error)
}
};task.resume()
}
}
单元格的 .xib 都已正确链接。使用断点进行故障排除时,第二类获取 HTTP 请求,但未填充数据。因此,当我在 cell.configure.... (第一类)设置断点时,它会遍历并填充 .xib 的每个条目。但是在第二类中它甚至没有得到配置功能。这是有效的配置功能
func configure(with model: TemperatureDataModel){
self.TempLabel.text = "\(Int(model.temperature))°"
self.HumidityLabel.text = "\(Int(model.humidity))%"
self.dayLabel.text = "\(String(model.timeCaptured))"
if #available(iOS 13.0, *) {
self.iconImageView.image = UIImage(systemName: "sun.max")
} else {
// Fallback on earlier versions
}
这是未达到的配置功能
func configure(with model: WindDataModel){
self.KnotsLabel.text = "\(Int(model.windSpeed_Knots))"
self.MPHLabel.text = "\(Int(model.windSpeed_MPH))"
self.DirectionLabel.text = "\(String(model.windDirection))"
if #available(iOS 13.0, *) {
self.iconImageView.image = UIImage(systemName: "wind")
} else {
// Fallback on earlier versions
}
}
有任何想法吗?
解决方案
推荐阅读
- sql - SQL - 每小时选择最多数量的类别
- css - 从 antd 设计中删除页脚后的样式问题
- openlayers - Openlayers 6.9.0:更改鼠标位置坐标的位置
- java - 如何避免将服务作为参数传递
- sql - 将数据导入 R 时,雪花日期偏移 1 天
- react-native - ReactNative PanResponder - 限制移动
- scala - coursier 发布 scala 3.1.0
- excel - 将错误输入的 MDY General Format 转换为 DMY UK Date Format 从一列到另一列
- python - 我正在尝试导入用户定义的函数,但出现此错误,我在此代码中做错了什么?
- django - 如何访问 django 中的链接表