在UICollectionView上获取NSIndexPath上的索引

D4r*_*iNS 3 xcode nsindexpath ios uicollectionview

我有一个UICollectionView的项目数组(有2列和3行),我想用indexPath访问它但我不能使用它

如果我使用indexPath.rowindexPath.item我得到0,1,0,1,0,1.

如果我使用indexPath.section我得到0,0,1,1,2,2

我想要的是0,1,2,3,4,5,6

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

MenuCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
//cell.backgroundColor = [UIColor redColor ];
cell.backgroundColor = [UIColor whiteColor ];

UIImage* imagenprobar =[UIImage imageNamed: [imagenes objectAtIndex: indexPath.item]];
[cell.imagen setImage: imagenprobar];




cell.label.text = [textos objectAtIndex: indexPath.item];

return cell;
Run Code Online (Sandbox Code Playgroud)

}

Par*_*iya 5

使用(indexpath.section*yourTotalColumn)+indexPath.row计算阵列的确切索引进行UICollectionView.

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{
   ......................

   .......................

   //Now calculate index of array (datasource) Note : here column is 2.
   NSInteger index = (indexpath.section*2)+indexPath.row
   cell.label.text = [textos objectAtIndex:index];

   return cell;
Run Code Online (Sandbox Code Playgroud)

}