简体   繁体   English

如何以编程方式将CollectionView滚动到底部

[英]How to Scroll CollectionView to bottom Programmatically

I used this code for scrolling the collection View: 我使用此代码滚动集合View:

let section = (self.collectionView?.numberOfSections)! - 1;
let item = (self.collectionView?.numberOfItems(inSection: section))! - 1;
let lastIndexPath = IndexPath(item: item, section: section);
self.collectionView?.scrollToItem(at: lastIndexPath, at: .bottom, animated: true);

But I get error : 但我得到错误:

* Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]' *由于未捕获的异常'NSRangeException'终止应用程序,原因:'* - [__ NSArrayM objectAtIndex:]:索引1超出边界[0 .. 0]'

let lastItemIndex = NSIndexPath(forItem: data.count - 1, inSection: 0)
collectionView?.scrollToItemAtIndexPath(lastItemIndex, atScrollPosition: .Bottom, animated: true)

dont forget about: 别忘了:
- atScrollPosition: .Bottom - atScrollPosition:.Bottom
- and you need to check is data.count > 0 beacause if data.count == 0 you will receive same error as you have - 你需要检查是data.count > 0因为if data.count == 0你将收到与你有相同的错误

Swift 4+ Swift 4+

extension UICollectionView {
    func scrollToLast() {
        guard numberOfSections > 0 else {
            return
        }

        let lastSection = numberOfSections - 1

        guard numberOfItems(inSection: lastSection) > 0 else {
            return
        }

        let lastItemIndexPath = IndexPath(item: numberOfItems(inSection: lastSection) - 1,
                                          section: lastSection)
        scrollToItem(at: lastItemIndexPath, at: .bottom, animated: true)
    }
}

You can animate your CollectionView to Bottom with the contentOffset also 您还可以使用contentOffsetCollectionView为Bottom

Here is an example 这是一个例子

let contentHeight: CGFloat = myCollectionView.contentSize.height
let heightAfterInserts: CGFloat = myCollectionView.frame.size.height - (myCollectionView.contentInset.top + myCollectionView.contentInset.bottom)
if contentHeight > heightAfterInserts {
    myCollectionView.setContentOffset(CGPoint(x: 0, y: myCollectionView.contentSize.height - myCollectionView.frame.size.height), animated: true) 
}
func scrollToItem(at: IndexPath, at: UICollectionViewScrollPosition, animated: Bool)

Can scroll the collection view contents until the specified item is visible. 可以滚动集合视图内容,直到指定的项目可见。 If item is not visible, you will get exception like Terminating app due to uncaught exception 'NSRangeException', reason: '... 如果item不可见,则会因为未捕获的异常'NSRangeException'而终止应用程序,例如终止应用程序,原因:'...

var xItem:Int = 0
var currentIndex:NSIndexPath!
@IBOutlet weak var rateCollection: UICollectionView!

@IBAction func btnNextTapped(_ sender: UIButton)
    {
        xItem = xItem+1
        if xItem != arrayRateModel.count
        {
        currentIndex = NSIndexPath(item: xItem, section: 0)
        rateCollection.scrollToItem(at: currentIndex as IndexPath, at: .centeredHorizontally, animated: true)
        }   
    }

Objective-C Objective-C的

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM