Is there a way to automatically scroll to the bott

2019-03-21 12:57发布

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).

I've found the method scrollToItemAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UICollectionViewScrollPosition)scrollPosition animated:(BOOL)animated

But now I'm getting stuck at trying to find the indexPath of the last object in the CollectionView.

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.

Is there a way to get the IndexPath for that last object in the CollectionView?

5条回答
你好瞎i
2楼-- · 2019-03-21 13:14

Here is your answer... If you don't want to ask datasource.

Add it in your viewDidAppear: method.

NSInteger section = [_collectionView numberOfSections] - 1 ;
NSInteger item = [_collectionView numberOfItemsInSection:section] - 1 ;
NSIndexPath *lastIndexPath = [NSIndexPath indexPathForItem:item inSection:section] ;
[_collectionView scrollToItemAtIndexPath:lastIndexPath atScrollPosition:(UICollectionViewScrollPositionBottom) animated:YES];
查看更多
forever°为你锁心
3楼-- · 2019-03-21 13:22

I would check first if theres is an option to scroll to bottom just in case something goes wrong with your data and your table/collection view doesn't have rows.

func scrollToBottomIfPossible() {

    guard self.myRows.count > 0 else {
            return
    }

    self.collectionView.scrollToItem(at: IndexPath(row: yourItems.count - 1, section: mySections.count), at: .bottom, animated: true)
}
查看更多
Rolldiameter
4楼-- · 2019-03-21 13:23

Swift 3 & 4, assuming you only have one section :

func scrollToBottom() {
    let section = 0
    let item = collectionView.numberOfItems(inSection: section) - 1
    let lastIndexPath = IndexPath(item: item, section: section)
    collectionView.scrollToItem(at: lastIndexPath, at: .bottom, animated: false)
}
查看更多
闹够了就滚
5楼-- · 2019-03-21 13:25

I was having an issue where I could scroll to the last item in the collection view, but was unable to scroll to the bottom of the footer.

The following code fixed that issue:

let bottomOffset = CGPoint(x: 0, 
                           y: collectionView.frame.height + (collectionView.contentSize.height * CGFloat(itemsInCollectionView.count)))
collectionView.setContentOffset(bottomOffset, animated: false)
查看更多
▲ chillily
6楼-- · 2019-03-21 13:29

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];
查看更多
登录 后发表回答