我在迅速新。我有使在笔尖文件的CollectionView和我的是,在主视图控制器一个子视图。我想表现出的CollectionView数组,但我不能。首先是让天的模型:
struct Days {
let day: String
let Image: String
let temp: Double
}
然后在daycell:
class DayCell: UICollectionViewCell {
@IBOutlet weak var lblDay: UILabel!
@IBOutlet weak var imgWeather: KBImageView!
@IBOutlet weak var lblTemp: UILabel!
func updateViews(day: Days) {
lblDay.text = day.day
imgWeather.setImageWithKingFisher(url: day.Image)
lblTemp.text = String(day.temp)
}
}
那么在公共类,我得到alamofire JSON数据与译码,并把它们在我的模型:
public class Publics {
static let instance = Publics()
func showInfo(code: String, completion: @escaping ([Days]) -> Void) {
let DaysUrl = "http://api.openweathermap.org/data/2.5/forecast?id=\(code)&appid=3e28385cde03f6ee26c83b629ca274cc"
Alamofire.request(DaysUrl, method: .get, parameters: nil, encoding: URLEncoding.httpBody).responseJSON { response in
if let data = response.data {
do {
self.myJson = try JSONDecoder().decode(JsonForecast.Response.self, from: data)
let counter = (self.myJson?.list.count)! - 1
let myDay1 = self.myJson?.list[counter-32]
let myDay2 = self.myJson?.list[counter-24]
let myDay3 = self.myJson?.list[counter-16]
let weekDay1 = self.getDate(date: self.getDayOfWeek((myDay1?.dt_txt)!)!)
let weekDay2 = self.getDate(date: self.getDayOfWeek((myDay2?.dt_txt)!)!)
let weekDay3 = self.getDate(date: self.getDayOfWeek((myDay3?.dt_txt)!)!)
let DaysArray = [
Days(day: weekDay1, Image: (myDay1?.weather[0].icon)!, temp: (myDay1?.main?.temp)!) ,
Days(day: weekDay2, Image: (myDay2?.weather[0].icon)!, temp: (myDay2?.main?.temp)!) ,
Days(day: weekDay3, Image: (myDay3?.weather[0].icon)!, temp: (myDay3?.main?.temp)!)
]
completion(DaysArray)
} catch {
print(error)
}
}
}
}
到这里我没有问题,但现在我想显示的CollectionView DaysArray,但我不能和我的CollectionView类如下:
class DayCollection: UIView, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
@IBOutlet var contentView: UIView!
@IBOutlet weak var collectionDay: UICollectionView!
var days = [Days]()
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "DayCell", for: indexPath) as! DayCell
Publics.instance.showInfo(code: "112931") { result in
self.days = result
print(self.days)
DispatchQueue.main.async {
self.collectionDay.reloadData()
}
}
let day = days[indexPath.item]
cell.updateViews(day: day)
return cell
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return days.count
}
override func awakeFromNib() {
super.awakeFromNib()
self.collectionDay.dataSource = self
self.collectionDay.delegate = self
self.collectionDay.register(UINib(nibName: "DayCell", bundle: nil), forCellWithReuseIdentifier: "DayCell")
}
}
我应该在mainVC类呢? (也许我应该i。从协议委托或没有使用?)
如果你想拥有首先在一个结构常数声明为常数。 private(set)
是可怕的。
struct Days {
let day: String
let dImage: String
let temp: Double
}
而永远宣告结构成员作为与非可选值在init
方法初始化隐含展开自选。反正不需要在结构中的init方法。
您必须添加一个完成处理程序
public func showInfo(code: String, completion: @escaping ([Days]) -> Void) {
...
let daysArray = [
Days(day: weekDay1, Image: (myDay1?.weather[0].icon)!, temp: (myDay1?.main?.temp)!) ,
Days(day: weekDay2, Image: (myDay2?.weather[0].icon)!, temp: (myDay2?.main?.temp)!) ,
Days(day: weekDay3, Image: (myDay3?.weather[0].icon)!, temp: (myDay3?.main?.temp)!)
]
completion(daysArray)
}
然后在类的集合视图的添加数据源阵列
var days = [Days]()
并获得数据
Publics.instance.showInfo(code: "Foo") { result in
self.days = result
DispatchQueue.main.async {
self.collectionDay.reloadData()
}
}
和return days.count
在numberOfItemsInSection
另外力展开细胞
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "DayCell", for: indexPath) as! DayCell
如果代码崩溃,你做了一个设计错误。通过可选的绑定代码不会崩溃,但你看不到任何东西,你不知道为什么
并获得了一天
let day = days[indexPath.item]
cell.updateViews(day)