Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ..

like image 799
Curnelious Avatar asked Jan 30 '26 08:01

Curnelious


1 Answers

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;
}
like image 149
Luca Bartoletti Avatar answered Feb 01 '26 23:02

Luca Bartoletti



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!