KMC*_*KMC 1 ios uicollectionview swift
我有两个集合视图,一个显示名称,另一个显示相应人员的年龄。此数据以“ [[[名称],“年龄”],[“名称”:“ Daniel”,“年龄”:“ 20”],[“名称”:“杰克”的形式存储在字典数组中,“ Age”:“ 20”]]。此数据来自CSV文件,因此第一个元素是标头。在collectionView cellForItemAtIndexPath内部,我正在检查集合视图并提供基于行号的数据,例如cell [indexPath.row] [“ Name”]和cell2 [indexPath.row] [“ Age”]。但是,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
}
}
Run Code Online (Sandbox Code Playgroud)
作为您的代码,您只设置numberOfItemsInSection了1,那么您总是得到第0个索引。使存在动态值,例如返回Array.count。
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.data.count // here you need to set dynamic count of array
}
Run Code Online (Sandbox Code Playgroud)
更新:
如果遵循,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
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2064 次 |
| 最近记录: |