2016-07-15 5 views
1

다음 4 일 동안 날씨 데이터를 표시하는 컬렉션보기가 있습니다.배열에서 하나의 인덱스 사용

[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 

내가 처음 일을 제거하고 다른 네 표시 :이 블록

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

에서이 x는처럼 보이는 방법이다. JSON에서 매 3 시간마다 기상 정보를 받았습니다. 배열이고 매일 하나의 데이터 만 표시하려고합니다.

어떻게 할 수 있습니까?

+0

에 대한 사용자 정의 셀을 더 읽으십시오. 해당 수집 셀에 쉽게 추출 할 수 있도록 json 응답을 추가하십시오. – xmhafiz

답변

1

는 수집보기에서 첫 번째, 당신은

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

여기에 동급 방법에서 액세스 할 수 있도록 그에 따라 클래스 변수해야 UICollectionViewDataSource

귀하의 데이터를 사용하여 데이터를 준비하고 다음을 채울 필요 당신의 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

관련 문제