在 UICollectionView 上获取 NSIndexPath 的索引

getting index on NSIndexPath on 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;

}

使用 (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;

}