2016-07-15 6 views
1

Ich habe eine Sammlung anzeigen wo Wetterdaten für die nächsten vier Tage anzeigen. In diesem BlockVerwenden Sie einen Index aus Array

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

Dies ist, wie x sucht wie:

[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 

ich den ersten Tag entfernen und die anderen vier anzuzeigen. Von JSON habe ich alle drei Stunden Wetterdaten bekommen. Es ist ein Array und ich möchte nur eine Daten für jeden Tag anzeigen.

Irgendwelche Vorschläge, wie man das macht?

+0

Bitte fügen Sie Ihre JSON-Antwort, um leicht in diese Sammlung Zellen zu extrahieren – xmhafiz

Antwort

1

In Sammlung Ansicht, zuerst, müssen Sie Ihre Daten erstellen und es dann entsprechend UICollectionViewDataSource

Ihre Daten füllen verwenden, sollte Klassenvariablen sein, so dass es von allen Methoden in der Klasse

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

Hier zugänglich ist Sie ist 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 
    } 
} 

Es gibt einfache Anleitung here. Versuchen Sie auch, lesen Sie mehr auf benutzerdefinierte Zelle für die

Verwandte Themen