Get reference to a specific cell in UIcollectionview

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

firstly, can you move a specific cell within the collection?

second, how would I check if there is one visible right now? (cells can be reused).

ERROR: When I do this I also get 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 int conversion is prohibited in ARC".

When its value is 0, I always get position 0, even if the cell is off-screen.

I am guessing I am missing the correct way to get the first cell.

+3


source to share


2 answers


You must instantiate 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;
}

      

+7


source


Swift 3 version code: Based on Luca Bartoletti's answer



func scrollViewDidScroll(scrollView: UIScrollView) {

    let indexPath = IndexPath(item: 4, section: 0)
    var cell = self.collectionView.cellForItem(at: indexPath)
    var cellRect = cell.frame
}

      

+1


source







All Articles