【问题标题】:How to scroll UICollectionView to the bottom programatically?如何以编程方式将 UICollectionView 滚动到底部?
【发布时间】:2015-07-21 20:16:29
【问题描述】:

我有一个UICollectionView。我想在视图出现时滚动到底部。如何做到这一点?

有人可以帮我解决这个问题吗? 提前致谢。

【问题讨论】:

    标签: ios objective-c uiscrollview uicollectionview


    【解决方案1】:

    您可以使用以下代码以编程方式将UICollectionView 滚动到底部

    NSInteger section = [self numberOfSectionsInCollectionView:collectionView] - 1;
    NSInteger item = [self collectionView:collectionView numberOfItemsInSection:section] - 1;
    NSIndexPath *lastIndexPath = [NSIndexPath indexPathForItem:item inSection:section];
    [collectionView scrollToItemAtIndexPath:lastIndexPath atScrollPosition:UICollectionViewScrollPositionBottom animated:YES];
    

    【讨论】:

      【解决方案2】:
      NSIndexPath *lastItem = /*Indexpath of last item*/;
      [self.collectionView scrollToItemAtIndexPath:lastItem atScrollPosition:UICollectionViewScrollPositionBottom animated:YES];
      

      【讨论】:

        【解决方案3】:

        你把这段代码滑下来。

        NSArray *visibleItems = [self.collectionView indexPathsForVisibleItems];
          NSIndexPath *currentItem = [visibleItems objectAtIndex:0];
          NSIndexPath *nextItem = [NSIndexPath indexPathForItem:currentItem.item + 1 inSection:currentItem.section];
          [self.collectionView scrollToItemAtIndexPath:nextItem atScrollPosition:UICollectionViewScrollPositionTop animated:YES];
        

        【讨论】:

          【解决方案4】:

          设置集合视图对象的contentoffset.y

          【讨论】:

            猜你喜欢
            • 2011-12-22
            • 1970-01-01
            • 1970-01-01
            • 2010-10-31
            • 2018-11-22
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-01-30
            相关资源
            最近更新 更多