11

Im using a UICollectionView and using a UIButton to scroll from cell to cell. I want the button.hidden = YES when I come to the end of the collection view. How do I know when currentIndex == MAX

000
  • 225
  • 1
  • 3
  • 17
  • Just like table view, implement scroll view s delegates to check if collection view got scrolled to bottom. http://stackoverflow.com/questions/5137943/how-to-know-when-uitableview-did-scroll-to-bottom-in-iphone – GoodSp33d Feb 23 '15 at 09:58

7 Answers7

8

A collection view is a scroll view. You therefore have access to all of the scroll view delegate methods - scrollViewDidScroll: will be called every time the scroll view moves, you can check at that point if you've scrolled to the bottom, or end, or wherever.

Note that the contentOffset property will refer to the origin of the visible scroll area, so it's probably simplest to check something like this:

if (CGRectGetMaxY(scrollView.bounds) == scrollView.contentSize.height) {
    button.hidden = YES;
}

This delegate method would not be called if you scrolled the view yourself, however - it would only apply if the view was scrolled by the user. You'd need to either call this yourself at the end of your automatic scrolling code or have similar logic in your automatic scrolling code to check this yourself.

jrturton
  • 118,105
  • 32
  • 252
  • 268
4

You can know by having an if statement to check if the current indexPath is showing the last object in your data source array.

if(indexPath.row == [dataSourceArray count]){
//Last cell was drawn
}
RJiryes
  • 951
  • 10
  • 25
4

You can simply detect when we are the bottom of the collection view and do something like this..

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    // getting the scroll offset
    CGFloat bottomEdge = scrollView.contentOffset.y + scrollView.frame.size.height;

    if (bottomEdge >= scrollView.contentSize.height)
    {
        // we are at the bottom
        button.hidden = YES;
    }
}

You can do whatever you want when you get to the bottom of the scrollView in you collection view..

valbu17
  • 4,034
  • 3
  • 30
  • 41
  • when I implement this method app gets crashed right after fetching data from server @NorthBlast. It gives NSRangeException – Mansuu.... Oct 13 '17 at 14:18
4

Here's my code, displaying when the last cell starts appearing (Swift 4):

override func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
    if indexPath.row == dataArray.count - 1 {
        // do something
    }
}
Skoua
  • 3,373
  • 3
  • 38
  • 51
1

Another way is to detect if collectionView:cellForItemAtIndexPath reaches the last section or last row.

For example, you can have two sections, one section to display stuff and the other as an indicator if current view is scrolled to the end:

public func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        if indexPath.section == 2 { // or if indexPath.row == myDat.count
            // CollectionView is scrolled to the bottom
            self.state = .Loading
            // Do something
        }
    }

Don't forget to set and reset view state to avoid being trapped in one state:

public func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
    return self.state == .Loading ? 1 : 2
}

And, you can also do the detection in other UICollectionViewDelegate methods, for example:

func collectionView(collectionView: UICollectionView, willDisplayCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
    if indexPath.row == yourData.count{
        self.state = .Loading
        // Do something
    }
}
Shijing Lv
  • 6,286
  • 1
  • 20
  • 12
0

I think any of these will help you

1.This scrollview delegate will call whenever scrolling movement comes to a halt or bottom

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {

    for (UICollectionViewCell *cell in [self.collectionView visibleCells]) {
        NSIndexPath *indexPath = [self.collectionView indexPathForCell:cell];
        NSUInteger lastIndex = [indexPath indexAtPosition:[indexPath length] - 1];
    }
}

or

  1. you can use something like this to check the last index of collection view

    NSInteger section = [self numberOfSectionsInCollectionView:self.collectionView] - 1;
    NSInteger item = [self collectionView:self.collectionView numberOfItemsInSection:section] - 1;
    NSIndexPath *lastIndexPath = [NSIndexPath indexPathForItem:item inSection:section];
    
itsji10dra
  • 4,603
  • 3
  • 39
  • 59
Anshad Rasheed
  • 2,526
  • 1
  • 14
  • 32
0

Swift 4 Version

override func collectionView(_ collectionView: UICollectionView, willDisplaySupplementaryView view: UICollectionReusableView, forElementKind elementKind: String, at indexPath: IndexPath) {

            if (elementKind == UICollectionElementKindSectionHeader) {
                view.layer.zPosition = 0;
            }
    }
Saranjith
  • 11,242
  • 5
  • 69
  • 122