Search code examples
iosobjective-ccollectionview

How can I get last object in CollectionView willDisplayCell?


How can I detect UICollectionView last visible object in willDisplayCell? In UITableView I can do it by using following code:-

-(void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if([indexPath row] == ((NSIndexPath*)[[tableView indexPathsForVisibleRows] lastObject]).row){
    //DO SOMETHING HERE
    }

}

Then for UICollectionView, how can I do this?

-(void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath{
    {
      //HOW CAN I DO THIS SAME LIKE TABLEVIEW?
    }
}

Solution

  • Its exactly the same :-

    -(void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath{     
         if([indexPath row] == ((NSIndexPath*)[[collectionView indexPathsForVisibleItems] lastObject]).row){
             //DO SOMETHING HERE
         }       
    }