【发布时间】:2020-06-27 15:49:29
【问题描述】:
我的 UITableViewCells 有问题
我创建了两个 UITableView 类。现在我想让它显示在 TableView 中。作为数据,我采用了一个天气 API,它应该向我显示两个不同 UITableViewCellen 中的温度和最低温度
但是,作为第 39 行的错误:
Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value
这是我的完整代码:
import UIKit
import Foundation
class FirstViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell") as! CustomCell
let minus: Double = 32.00
let session = URLSession.shared
let weatherURL = URL(string: "https://api.openweathermap.org/data/2.5/weather?q=Ismaning&appid=2da51d7209b1151fc1bf22e761c88d4e")!
let dataTask = session.dataTask(with: weatherURL) {
(data: Data?, response: URLResponse?, error: Error?) in
if let error = error {
print("Error:\n\(error)")
} else {
if let data = data {
let dataString = String(data: data, encoding: String.Encoding.utf8)
print("All the weather data:\n\(dataString!)")
if let jsonObj = try? JSONSerialization.jsonObject(with: data, options: .allowFragments) as? NSDictionary {
if let mainDictionary = jsonObj.value(forKey: "main") as? NSDictionary {
if var temperature = mainDictionary.value(forKey: "temp") {
temperature = (temperature as! Double - minus) / 1.8 / 10
DispatchQueue.main.async {
cell.aktuelleTemperatur.text = "\(temperature)°C"
}
}
} else {
print("Error: unable to find temperature in dictionary")
}
} else {
print("Error: unable to convert json data")
}
} else {
print("Error: did not receive data")
}
}
}
dataTask.resume()
return cell
}
else if indexPath.row == 1 {
let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCellNr2") as! CustomCellNr2
let minus: Double = 32.00
let session = URLSession.shared
let weatherURL = URL(string: "https://api.openweathermap.org/data/2.5/weather?q=Ismaning&appid=2da51d7209b1151fc1bf22e761c88d4e")!
let dataTask = session.dataTask(with: weatherURL) {
(data: Data?, response: URLResponse?, error: Error?) in
if let error = error {
print("Error:\n\(error)")
} else {
if let data = data {
let dataString = String(data: data, encoding: String.Encoding.utf8)
print("All the weather data:\n\(dataString!)")
if let jsonObj = try? JSONSerialization.jsonObject(with: data, options: .allowFragments) as? NSDictionary {
if let mainDictionary = jsonObj.value(forKey: "main") as? NSDictionary {
if let temperature = mainDictionary.value(forKey: "temp") {
let temperature1: Double? = (temperature as! Double - minus) / 1.8 / 10
DispatchQueue.main.async {
cell.mindestTemperatur.text = String(format:"%.f", temperature1!) + "°C"
}
}
} else {
print("Error: unable to find temperature in dictionary")
}
} else {
print("Error: unable to convert json data")
}
} else {
print("Error: did not receive data")
}
}
}
dataTask.resume()
return cell
}
else {
let cell: UITableViewCell = UITableViewCell(style: UITableViewCell.CellStyle.default, reuseIdentifier: "thirdCustomCell")
//set the data here
return cell
}
}
}
谁能帮我解决这个问题?
提前谢谢你
【问题讨论】:
-
在
cellForRowAt中运行异步任务是荒谬的。
标签: ios json swift uitableview request