有没有办法自动滚动到UICollectionView的底部 [英] Is there a way to automatically scroll to the bottom of a UICollectionView
问题描述
所以我正在开发一个项目,它有一个按钮,可以将单元格添加到UICollectionView,然后需要自动滚动到最后一个单元格(即UICollectionView的底部)。
So I'm currently working on a project that has a button that adds cells to a UICollectionView and then needs to automatically scroll to the last cell (i.e. the bottom of the UICollectionView).
我发现方法
scrollToItemAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UICollectionViewScrollPosition)scrollPosition animated:(BOOL)animated
但现在我不得不试图找到CollectionView中最后一个对象的 indexPath
。
But now I'm getting stuck at trying to find the indexPath
of the last object in the CollectionView.
问题似乎在于我一直在试图将UICollectionView中的单元格视为一个数组(不能通过它们枚举,不响应到 lastObject
等等)。我似乎最接近的是方法 visibleItems
哪个 给我一个数组,但是当我需要在外面添加的单元格时没有帮助可见框架。
The problem seems to lie in that I've been trying to think of the cells in the UICollectionView as an array (can't enumerate through them, doesn't respond to lastObject
and so on). The closest I can seem to get is the method visibleItems
which does give me an array but doesn't help when I need cells that are added outside of the visible frame.
有没有办法在CollectionView中获取最后一个对象的IndexPath?
Is there a way to get the IndexPath for that last object in the CollectionView?
推荐答案
您可以询问您的数据来源:
You can just ask your data source:
NSInteger section = [self numberOfSectionsInCollectionView:self.collectionView] - 1;
NSInteger item = [self collectionView:self.collectionView numberOfItemsInSection:section] - 1;
NSIndexPath *lastIndexPath = [NSIndexPath indexPathForItem:item inSection:section];
这篇关于有没有办法自动滚动到UICollectionView的底部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!