Arrays 使用数组中的一个索引

Arrays 使用数组中的一个索引,arrays,json,swift,uicollectionview,Arrays,Json,Swift,Uicollectionview,我有一个收集视图,其中显示未来四天的天气数据。在这个街区 func prepareCollectionViewDataSource() { var x = self.city?.forecasts x?.removeFirst() self.otherDaysForecasts = x } 这就是x的样子: [0] : Forecast - temperature : 296.84199999999998 { ... }

我有一个收集视图,其中显示未来四天的天气数据。在这个街区

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中,我每三个小时获得一次天气数据。这是一个数组,我希望每天只显示一个数据


有什么建议吗?

在集合视图中,首先需要准备数据,然后使用
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
    }
}
您的数据应该是类变量,以便可以从类中的任何方法访问

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
    }
}

这里有一个简单的指南。另外,请尝试阅读有关
UICollectionView

自定义单元格的更多信息。请添加json响应,以便轻松提取到这些集合单元格中