Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly get index from index path in a UICollectionView?

Tags:

ios

I have a one dimensional array of images that I am feeding to a UICollectionView. The collection view is just one section composed of the elements in the array. when I click a cell and -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath; gets fired off and I do a print out of the indexPath I get values like [0,0] for element 0 in row 0 but then the very next element in that row is [0,5].

How can I map the indexPath to my array like the way UITableView's [indexPath row] matches directly to the corresponding index?

like image 242
Nirma Avatar asked Feb 25 '13 14:02

Nirma


People also ask

How do you get the indexPath row when a button in a cell is tapped?

add an 'indexPath` property to the custom table cell. initialize it in cellForRowAtIndexPath. move the tap handler from the view controller to the cell implementation. use the delegation pattern to notify the view controller about the tap event, passing the index path.

What is index path in Swift?

A list of indexes that together represent the path to a specific location in a tree of nested arrays.

What is indexPath section Swift?

In Swift, an indexPath is a list of indexes that, together, represent the path to a specific location in a tree of nested arrays. It describes an item's position inside a table view or collection view, storing both its section and its position inside that section.


2 Answers

To Retrieve item index you need to use indexPath.item(for UICollectionView) not indexPath.row(it's for UITableView)

like image 187
Dinesh Reddy Chennuru Avatar answered Sep 27 '22 22:09

Dinesh Reddy Chennuru


I've never used them, but my understanding is that the two properties of a UICollectionView's indexPath are section and item, not section and row like in a UITableView.

like image 37
Darren Avatar answered Sep 28 '22 00:09

Darren