Recycled Steel Recycled Steel - 1 year ago 117
iOS Question

Identify Cell after Tap Location in UICollectionView

I have attached a UILongPressGestureRecognizer to a Collection View with the aim of the user hold finger on a cell in the table and being asked if they want to remove it.

The problem I have is that I can not get the indexPath like I would with a table cell (using indexPathForRowAtPoint), is there an equivalent?

If not I have thought about adding the gesture to each cell, but then how do I identify the which gesture (I can not see a "tag" for a gesture), e.g. can I send indexPath.item in the selector? Or some other method?

Answer Source

You got the same thing on the UICollectionView. According to the documentation:

indexPathForItemAtPoint: Returns the index path of the item at the specified point in the collection view.

  • (NSIndexPath *)indexPathForItemAtPoint:(CGPoint)point Parameters point A point in the collection view’s coordinate system. Return Value The index path of the item at the specified point or nil if no item was found at the specified point.

Discussion This method relies on the layout information provided by the associated layout object to determine which item contains the point.

Availability Available in iOS 6.0 and later. Declared In UICollectionView.h

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download