【发布时间】:2020-02-05 23:22:55
【问题描述】:
该函数没有被调用并且没有显示任何数据。我的代码在下面,我没有在控制台中打印任何错误。我在函数上设置了一个断点,但它没有捕获。
我之前遇到索引越界崩溃,但更改了一些代码,现在集合视图没有填充。
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"
}
【问题讨论】:
标签: swift uicollectionview cell