【发布时间】:2018-06-14 07:54:48
【问题描述】:
我正在尝试使用 Open Weather API 构建一个天气预报应用程序,并且在我的预测类中,我有以下结构类型。
struct ForecastWeather {
public private(set) var date: String
public private(set) var weatherDetails: [WeatherInfo]
}
struct WeatherInfo {
public private(set) var weatherImage: String
public private(set) var time: String
public private(set) var weatherType: String
public private(set) var temperature: String
}
我需要使用二维数组,因为在我的表格视图中有预测数据的部分。我将对象放入var forecastWeathers = [ForecastWeather]() 数组中。
ForecastWeather(date: "Thursday", weatherDetails: [WeatherForecast.WeatherInfo(weatherImage: "01d", time: "12:00", weatherType: "Clear", temperature: "35.0")])
ForecastWeather(date: "Thursday", weatherDetails: [WeatherForecast.WeatherInfo(weatherImage: "02d", time: "15:00", weatherType: "Clouds", temperature: "31.0")])
ForecastWeather(date: "Thursday", weatherDetails: [WeatherForecast.WeatherInfo(weatherImage: "01n", time: "18:00", weatherType: "Clear", temperature: "21.0")])
ForecastWeather(date: "Friday", weatherDetails: [WeatherForecast.WeatherInfo(weatherImage: "03n", time: "21:00", weatherType: "Clouds", temperature: "16.0")])
ForecastWeather(date: "Friday", weatherDetails: [WeatherForecast.WeatherInfo(weatherImage: "10n", time: "00:00", weatherType: "Rain", temperature: "13.0")])
ForecastWeather(date: "Friday", weatherDetails: [WeatherForecast.WeatherInfo(weatherImage: "10d", time: "03:00", weatherType: "Rain", temperature: "14.0")])
ForecastWeather(date: "Monday", weatherDetails: [WeatherForecast.WeatherInfo(weatherImage: "10d", time: "12:00", weatherType: "Rain", temperature: "19.0")])
ForecastWeather(date: "Monday", weatherDetails: [WeatherForecast.WeatherInfo(weatherImage: "10d", time: "15:00", weatherType: "Rain", temperature: "19.0")])
ForecastWeather(date: "Monday", weatherDetails: [WeatherForecast.WeatherInfo(weatherImage: "03n", time: "18:00", weatherType: "Clouds", temperature: "16.0")])
在我的表格视图中,我需要像WeatherService.instance.forecastWeathers[indexPath.section].weatherDetails[indexPath.row] 一样将这些部分放入其中。它现在给了我一个错误,因为我里面有 3 个相同的星期四对象。如何映射或过滤仅包含一次日期信息的 forecastWeathers 数组,因为我有多个部分我尝试使用映射功能但我无法成功。
在这里编辑是我的 API 调用
let json = JSON(value)
for i in 0..<json["list"].count {
let icon = json["list"][i]["weather"][0]["icon"].stringValue // Image icon
let date = self.convertUnixDate(fromDoubleTo: json["list"][i]["dt"].doubleValue)
let weatherType = json["list"][i]["weather"][0]["main"].stringValue.capitalized
let temperature = "\(self.temperatureConverter(kelvinTo: json["list"][i]["main"]["temp"].doubleValue))"
let time = self.convertTime(timeArr: json["list"][i]["dt_txt"].stringValue)
let forecastObj = ForecastWeather(date: date, weatherDetails: [WeatherInfo(weatherImage: icon, time: time, weatherType: weatherType, temperature: temperature)])
print(forecastObj)
self.forecastWeathers.append(forecastObj)
}
还有API结果5 day forecast
这是我的表格视图委托方法
func numberOfSections(in tableView: UITableView) -> Int {
return WeatherService.instance.newForecastWeathers.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return WeatherService.instance.newForecastWeathers[section].date.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = forecastTableView.dequeueReusableCell(withIdentifier: cellId) as? ForecastCell else { return ForecastCell() }
cell.configureCell(weatherDetails: WeatherService.instance.newForecastWeathers[indexPath.section].weatherDetails[indexPath.row])
return cell
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let view = UIView()
view.backgroundColor = .orange
let label = UILabel()
label.text = WeatherService.instance.newForecastWeathers[section].date
label.frame = CGRect(x: 45, y: 5, width: 100, height: 35)
view.addSubview(label)
return view
}
【问题讨论】:
-
看我的回答
-
我已经更新了答案
标签: ios arrays swift uitableview