gpt4 book ai didi

ios - indexPath.row 总是返回零

转载 作者:搜寻专家 更新时间:2023-11-01 05:58:12 29 4
gpt4 key购买 nike

我有两个 Collection View ,一个显示姓名,另一个显示相应人员的年龄。此数据以 "[["Name","Age"],["Name": "Daniel", "Age": "20"],["Name":"Jake"] 的形式存储在字典数组中"Age":"20"]]。此数据来自 CSV 文件,因此第一个元素是标题。在 collectionView cellForItemAtIndexPath 内部,我正在检查 Collection View 并提供基于行号的数据库,如 cell[indexPath.row] ["Name"] 和 cell2[indexPath.row]["Age"]。但是,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
}

}

最佳答案

作为您的代码,您将 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
}

更新:

如果您遵循了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
}

}

关于ios - indexPath.row 总是返回零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32797211/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com