When the "UICollectionView" came to an end

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

+6
source share
6 answers

A collection view is a scroll view. Thus, you have access to all methods of the scroll delegate - scrollViewDidScroll: will be called every time the scroll move moves, you can check this if you scroll from the bottom, or finish, or anywhere.

Note that the contentOffset property will refer to the beginning of the visible contentOffset area, so it's probably the easiest way to check for something like this:

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

This delegate method will not be called if you have scrolled the view yourself, however - it only applies if the view has been scrolled by the user. You will need to either call it yourself at the end of your automatic scroll code, or have the same logic in your automatic scroll code to check it yourself.

+7
source

You can find out by having an if statement to check if the current indexPath shows the last object in your array of data sources.

 if(indexPath.row == [dataSourceArray count]){ //Last cell was drawn } 
+3
source

You can simply determine when we are the bottom of the collection 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 end up at the bottom of scrollView in your collection view.

+3
source

Another way is to determine if collectionView:cellForItemAtIndexPath last section or last row.

For example, you can have two sections, one section for displaying material, and the other as an indicator, if the current view scrolls 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 } } 

Remember to set and reset to view the state to avoid falling into one state:

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

And you can also perform 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 } } 
+1
source

I think any of them will help you.

1.This scrollview delegate will call whenever the scroll motion stops or decreases

 - (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 latest collection view index

     NSInteger section = [self numberOfSectionsInCollectionView:self.collectionView] - 1; NSInteger item = [self collectionView:self.collectionView numberOfItemsInSection:section] - 1; NSIndexPath *lastIndexPath = [NSIndexPath indexPathForItem:item inSection:section]; 
0
source

version of Swift 4

 override func collectionView(_ collectionView: UICollectionView, willDisplaySupplementaryView view: UICollectionReusableView, forElementKind elementKind: String, at indexPath: IndexPath) { if (elementKind == UICollectionElementKindSectionHeader) { view.layer.zPosition = 0; } } 
0
source

Source: https://habr.com/ru/post/982878/


All Articles