我有兩個集合視圖和一個顯示名稱,另一個顯示相應人員的年齡。這些數據以「[[」Name「,」Age「],[」Name「:」Daniel「,」Age「:」20「],[」Name「:」Jake「 ,「Age」:「20」]]。這個數據來自於CSV文件,所以第一個元素是一個頭。在collectionView cellForItemAtIndexPath裏面,我正在檢查集合視圖並且提供數據庫像row [rowPath.row] [ 「名稱」]和小區2 [indexPath.row] [ 「時代」 然而,indexPath.row總是返回零,所以我想起來了頭 -indexPath.row總是返回零
如何解決這個問題?這是我的代碼 -
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的編號,那麼您是如何得到indexPath.row多於0的? –
嘗試將numberOfitems返回到self.data.count –