Curnelious Curnelious -4 years ago 140
Objective-C Question

Get reference to certain cell in UIcollectionview

I am trying to get a reference to the first cell in collection view in order to move it (for some effect) .

first, can you move a certain cell that is inside the collection ?

second,how would i check if its on visible rect right now? (cells are reusable) .

ERROR: when i do this, i am also getting an error when i set the index path to non zero

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:4];//E
CGRect cellRect = cell.frame;
NSLog(@"%f",cellRect.origin.y);



}


"implicit conversion of int is disallowed in ARC ".

When its set to 0 , i always get position of 0, even when the cell is offscreen.

I guess i am missing the right way to get the first cell ..

Answer Source

You should create an instance of an NSIndexPath to use cellForItemAtIndexPath

Example

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:4 inSection:0];
    UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:indexPath];
    CGRect cellRect = cell.frame;
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download