【问题标题】:When did `UICollectionView` come to an end`UICollectionView` 什么时候结束
【发布时间】:2015-04-24 14:29:54
【问题描述】:

我使用UICollectionViewUIButton 从一个单元格滚动到另一个单元格。 当我来到集合视图的末尾时,我想要 button.hidden = YES。我怎么知道 currentIndex == MAX

【问题讨论】:

标签: ios objective-c uiscrollview uibutton uicollectionview


【解决方案1】:

这是我的代码,显示最后一个单元格何时开始出现(Swift 4):

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

【讨论】:

    【解决方案2】:

    Swift 4 版本

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

    【讨论】:

      【解决方案3】:

      另一种方法是检测collectionView:cellForItemAtIndexPath 是否到达最后一节或最后一行。

      例如,您可以有两个部分,一个部分用于显示内容,另一个部分作为当前视图滚动到末尾时的指示器:

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

      不要忘记设置和重置视图状态以避免陷入一种状态:

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

      而且,您还可以在其他UICollectionViewDelegate方法中进行检测,例如:

      func collectionView(collectionView: UICollectionView, willDisplayCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
          if indexPath.row == yourData.count{
              self.state = .Loading
              // Do something
          }
      }
      

      【讨论】:

      • 这个条件永远不会成立@ShijingLv
      【解决方案4】:

      我认为这些都会对你有所帮助

      1.这个scrollview delegate将在滚动停止或底部时调用

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

      1. 您可以使用类似的方法来检查集合视图的最后一个索引

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

      【讨论】:

        【解决方案5】:

        您可以简单地检测我们何时处于集合视图的底部并执行类似的操作..

        - (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;
            }
        }
        

        当您到达集合视图中的滚动视图底部时,您可以做任何您想做的事情..

        【讨论】:

        • 当我实现这个方法时,应用程序在从服务器@NorthBlast 获取数据后立即崩溃。它给出了 NSRangeException
        【解决方案6】:

        集合视图是一个滚动视图。因此,您可以访问所有滚动视图委托方法 - 每次滚动视图移动时都会调用 scrollViewDidScroll:,此时您可以检查您是否滚动到底部、末尾或任何位置。

        请注意,contentOffset 属性将引用可见滚动区域的原点,因此检查如下内容可能最简单:

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

        但是,如果您自己滚动视图,则不会调用此委托方法 - 它仅适用于用户滚动视图时。您需要在自动滚动代码的末尾自己调用它,或者在自动滚动代码中使用类似的逻辑来自己检查。

        【讨论】:

        • 测试应该是>= 而不是== 以便轻松捕捉滚动:)
        【解决方案7】:

        您可以通过 if 语句检查当前 indexPath 是否显示数据源数组中的最后一个对象。

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

        【讨论】:

        • 在你的 cellForItemAtIndexPath 方法中。
        猜你喜欢
        • 2019-07-15
        • 1970-01-01
        • 2019-10-17
        • 1970-01-01
        • 1970-01-01
        • 2013-07-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多