Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

moonman239

Cancelled
Original poster
Mar 27, 2009
1,541
32
Relevant code in implementation file:

Code:
-(UICollectionViewCell *)visibleCellWhoseFrameContainsPoint:(CGPoint)point
{
    UICollectionViewCell *theCell;
    for (UICollectionViewCell *cell in [self visibleCells])
    {
        if (CGRectContainsPoint([cell frame], point)) {
            theCell = cell;
            break;
        }
    }
    return theCell;
}

"theCell" appears to be nil. When I set a breakpoint to evaluate "cell" I get multiple error messages that say "'frame' has unknown return type; cast the call to its declared return type"

After looking at this page on Stack Overflow, I believe the solution is to use the collection view's layoutAttributesForItemAtIndexPath: method.
 
Have a look on the method
Code:
- (NSIndexPath *)indexPathForItemAtPoint:(CGPoint)point

You give in the point in CollectionView-dimension and get the indexPath back.

And the cell if you need
Code:
- (UICollectionViewCell *)cellForItemAtIndexPath:(NSIndexPath *)indexPath

https://developer.apple.com/library...stm/UICollectionView/indexPathForItemAtPoint:

some snippet from an actual code I use in a UICollectionViewController

Code:
- (IBAction)handleLongPress:(id)sender

{
   UILongPressGestureRecognizer *pg = (UILongPressGestureRecognizer *)sender;
   
   // find the location
   CGPoint point = [pg locationInView:self.collectionView];

   // get the path for the cell underneath the point   
   NSIndexPath *idxPath = [self.collectionView indexPathForItemAtPoint:point];

   // ... much more code
}

But keep in mind that with the "aggressive" (or let say: consistent) reuse of cells the value is mainly good for limited scope; means don't try to store the cell reference in own lists for reuse; store the indexPath instead.
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.