UICollectionView cellForItemAtIndexPath indexPath row is nil

Anders picture Anders · Aug 9, 2013 · Viewed 7.2k times · Source

First time that I'm using UICollectionView. I'm trying to get the "row" value for each cell - to be used as an identifier/tag for the cells textfield. When cellForItemAtIndexPath gets called it seems like [indexPath row]; returns nil. How can I get the cell index (or something else to be used as an identifier) when row is nil?

Note Using iOS7, if that makes any difference...

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView  cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    // Returns nil
    NSLog("Row: %d", indexPath.row);
}

Any ideas/tips?

Answer

Bruno Koga picture Bruno Koga · Aug 9, 2013

You don't use the concept of row in a Collection View. You need to use this method from NSIndexPath:

+ (NSIndexPath *)indexPathForItem:(NSInteger)item inSection:(NSInteger)section;

Notice that the row property of a NSIndexPath is declared on UITableView.h, while the item property is declared on the UICollectionView.h