2015-09-26 143 views
1

我有兩個集合視圖和一個顯示名稱,另一個顯示相應人員的年齡。這些數據以「[[」Name「,」Age「],[」Name「:」Daniel「,」Age「:」20「],[」Name「:」Jake「 ,「Age」:「20」]]。這個數據來自於CSV文件,所以第一個元素是一個頭。在collectionView cellForItemAtIndexPath裏面,我正在檢查集合視圖並且提供數據庫像row [rowPath.row] [ 「名稱」]和小區2 [indexPath.row] [ 「時代」 然而,indexPath.row總是返回零,所以我想起來了頭 -indexPath.row總是返回零

enter image description here

如何解決這個問題?這是我的代碼 -

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

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    return 1 
} 

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 

    if collectionView == self.nameCollectionView { 
     let nameCell = collectionView.dequeueReusableCellWithReuseIdentifier("NameCell", forIndexPath: indexPath) as! NameCell 

     nameCell.data.text = self.data?[indexPath.row]["Name"] 
     println(indexPath.row) 

     return nameCell 
    } 
    else{ 
     let ageCell = collectionView.dequeueReusableCellWithReuseIdentifier("AgeCell", forIndexPath: indexPath) as! AgeCell 

     ageCell.data.text = self.data?[indexPath.row]["Age"] 



     return ageCell 
    } 

} 
+1

如果您正在設置項目1的編號,那麼您是如何得到indexPath.row多於0的? –

+0

嘗試將numberOfitems返回到self.data.count –

回答

4

因爲你的代碼你只設置numberOfItemsInSection然後你總是得到第0個索引。使例如返回Array.count有動態值。

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    return self.data.count // here you need to set dynamic count of array 
} 

UPDATE:

如果按照numberOfSectionsInCollectionView然後讓你的代碼像下面的cellForRow

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 

    if collectionView == self.nameCollectionView { 
     let nameCell = collectionView.dequeueReusableCellWithReuseIdentifier("NameCell", forIndexPath: indexPath) as! NameCell 

     nameCell.data.text = self.data?[indexPath.section]["Name"] 
     println(indexPath.section) 

     return nameCell 
    } 
    else{ 
     let ageCell = collectionView.dequeueReusableCellWithReuseIdentifier("AgeCell", forIndexPath: indexPath) as! AgeCell 

     ageCell.data.text = self.data?[indexPath.section]["Age"] 



     return ageCell 
    } 

} 
+0

當我這樣做時,我得到了多個不是我想要的列。 – user30646

+0

首先用你的邏輯清楚你想要什麼。和'numberOfItemsInSection'返回特定段中的行數。代表indexPath.row。和'numberOfSectionsInCollectionView',它返回代表indexPath.section的節的數量。現在你需要考慮和使用,因爲你需要 –

+0

這工作!謝謝! – user30646

0

正如其他人所指出的那樣,你是,你總是告訴你收集意見在每個部分有2個部分和1個項目。因此,收集視圖將只在每個部分中要求1個項目。因此,每個部分只有一個項目(索引0)。

你說「此數據存儲內部字典的形式......」

它是一本字典或詞典的陣列?字典是一個無序的集合,所以它不適合存儲一組有序的項目以供給集合視圖或表視圖。一系列字典合適。從你顯示的數據和你的代碼,看起來你有一個字典數組。你應該編輯你的問題來說明清楚。

你的代碼沒有意義。您有2個不同的收集視圖,並且每個視圖都顯示不同的數據。您告訴您的收藏視圖您有兩個部分,但忽略部分編號併爲兩部分創建相同的數據。那裏有什麼不對。

+0

對不起這是字典數組。我想要做的就是在一列中顯示姓名,並在另一列中陳年。 – user30646

0

IndexPath是一個屬性,它具有以下結構 Indexpath {Section, Row}。 所以如果你想你的數據在兩個不同的部分,在這些單行然後indexpath.row爲他們每個人是否會返回一個0作爲,因爲

對於部分指數0 - Indexpath[0,0]意義的部分索引0和行indexpath指數0

對於部分指數1 - Indexpath[1,0]意義的部分指標1和行標0

希望可以讓您瞭解indexpath。