2016-07-15 61 views
1

我有一個收集視圖,顯示接下來四天的天氣數據。在此塊使用陣列中的一個索引

func prepareCollectionViewDataSource() { 
     var x = self.city?.forecasts 
     x?.removeFirst() 
     self.otherDaysForecasts = x 
    } 

這是X是如何看起來像:

[0] : Forecast 
     - temperature : 296.84199999999998 { ... } 
     - maximum : 296.84199999999998 { ... } 
     - minimum : 296.84199999999998 { ... } 
     - description : "light rain" { ... } 
     - icon : "10d" { ... } 
     - humidity : 92.0 { ... } 
     - pressure : 1021.4299999999999 { ... } 
     - wind : 1.8600000000000001 { ... } 
     - date : 2016-07-18 18:00:00 +0000 

我刪除的第一天,並顯示其他四個。從JSON我每三個小時就得到一次天氣數據。這是一個數組,我想每天只顯示一個數據。

任何建議如何做到這一點?

+0

自定義單元格請添加您的JSON響應很容易提取到那些收集細胞 – xmhafiz

回答

1

在集合認爲,首先,你需要準備的數據,然後填充它相應的使用UICollectionViewDataSource

你的數據應該是類變量,所以它是從類中的任何方法

var forcastData = [] // this is your x, array of dictionary 

這裏訪問是你的UICollectionViewDataSource

extension YourViewController : UICollectionViewDataSource { 

    override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int { 
     return 1 
    } 

    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     return forcastData.count 
    } 

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
     let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! UICollectionViewCell 
     // Here is the main part to configure the cell 
     let data = forcastData[indexPath.row] 
     // let say you have label for temparature that we want to set from your data in json dictionary 
     cell.temperatureLabel.text = String(format: "%.2f MB", data.["temperature"].double!) 
     return cell 
    } 
} 

有簡單的指南here。也可以嘗試閱讀更多關於爲UICollectionView