PRAVEEN
PRAVEEN

Reputation: 5895

How to Scroll CollectionView to bottom Programmatically

I used this code for scrolling the collection 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]'

Upvotes: 10

Views: 16175

Answers (6)

Pranit
Pranit

Reputation: 930

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];

Upvotes: 0

Yuchen
Yuchen

Reputation: 33036

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

Upvotes: 17

Amul4608
Amul4608

Reputation: 1448

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

Upvotes: 0

Vadim Kozak
Vadim Kozak

Reputation: 420

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

dont forget about:
- atScrollPosition: .Bottom
- and you need to check is data.count > 0 beacause if data.count == 0 you will receive same error as you have

Upvotes: 7

Rajat
Rajat

Reputation: 11127

You can animate your CollectionView to Bottom with the contentOffset also

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

Upvotes: 4

Vlad Khambir
Vlad Khambir

Reputation: 4383

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: '...

Upvotes: 0

Related Questions