swift - 未调用 cellForItemAt 函数
问题描述
该函数没有被调用并且不显示任何数据。我的代码在下面,我没有在控制台中打印任何错误。我在函数上设置了一个断点,它没有捕获。
我以前遇到索引越界崩溃,但更改了一些代码,现在集合视图没有填充。
import UIKit
import CoreLocation
class ForecastController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
@IBOutlet weak var collectionView: UICollectionView!
let rainArray : [Int] = [1,2,3,4,5,6,7,8,9,10]
let cloudyArray : [Int] = [26,27,28,29,30]
let snowArray : [Int] = [13,14,15,16]
let sunnyArray : [Int] = [31,32,33,34]
let stormArray : [Int] = [41,42,43,44,45,46,47]
var Forecast : [Forecast] = []
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return Forecast.count
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize.init(width: 165, height: 200)
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ForecastCell.reuseId, for: indexPath) as? ForecastCell else {
return collectionView.dequeueReusableCell(withReuseIdentifier: "blank", for: indexPath)
}
let data = Forecast[indexPath.item]
cell.summary.text = data.text
cell.highLow.text = "\(data.high) | \(data.low)"
cell.dayLabel.text = data.day
if self.rainArray.contains(data.code) == true {
cell.weatherIcon.image = UIImage(named: "Rain.png")
} else if self.snowArray.contains(data.code) == true {
cell.weatherIcon.image = UIImage(named: "snow.png")
} else if self.cloudyArray.contains(data.code) == true {
cell.weatherIcon.image = UIImage(named: "cloudy.png")
} else if self.sunnyArray.contains(data.code) == true {
cell.weatherIcon.image = UIImage(named: "sunny.png")
} else if self.stormArray.contains(data.code) == true {
cell.weatherIcon.image = UIImage(named: "storm.png")
} else {
cell.weatherIcon.image = UIImage(named: "cloudy.png")
}
return cell
}
func getWeather() {
YahooWeatherAPI.shared.weather(lat: "\(currentLat!)", lon: "\(currentLong!)", failure: { (error) in
print(error.localizedDescription)
print("Error pulling weather data")
}, success: { (response) in
let data = response.data
do {
let weather = try JSONDecoder().decode(Weather.self, from: data)
for x in weather.forecasts {
// populate collection view
self.Forecast = [x]
}
} catch {
print(error)
}
}, responseFormat: .json, unit: .imperial)
}
override func viewDidLoad() {
super.viewDidLoad()
collectionView.dataSource = self
collectionView.delegate = self
getWeather()
// Do any additional setup after loading the view.
}
}
class ForecastCell: UICollectionViewCell {
@IBOutlet weak var weatherIcon: UIImageView!
@IBOutlet weak var dayLabel: UILabel!
@IBOutlet weak var summary: UILabel!
@IBOutlet weak var highLow: UILabel!
public static var reuseId : String = "forecastCell"
}
解决方案
reloadData()
从雅虎获得数据后,您应该致电:
do {
let weather = try JSONDecoder().decode(Weather.self, from: data)
for x in weather.forecasts {
// populate collection view
self.Forecast = [x]
}
collectionView.reloadData()
} catch {
print(error)
}
您的代表不知道数据已更改,因此cellForItemAt
不会被调用。
推荐阅读
- python - 我创建了一个自定义 Odoo 模块,当我想升级它时它显示解析错误
- http - 如果一个 IP 没有监听 HTTP(不是服务器)或离线,GET 请求参数是否仍然通过网络发送给他们?
- sqlalchemy - 使用共享模式的 SQLAlchemy 中基于行/列的多租户
- python - 让机器人在语音聊天 discord.py 中播放 youtube 视频
- node.js - 如何转换 Redis 字符串对象数据?
- java - SpringBoot .jar 在作为对另一个项目的依赖项(Spring MVC)注入时不会运行/启动
- javascript - Vue Google Maps 未定义 - 单标记错误
- jquery - Jquery 循环插件 - 多个 div 在 alt 之后有多个
- python - 如何使用熊猫将前导零转换为十进制?
- kubernetes - Kubernetes 迁移数据库和公司的最佳方式