How do I get the last indexPath of a uicollectionview (NOT TABLE VIEW) ? Need it do server side paging

Viewed 3753

I have looked for ways of getting the last indexPath of a UICollectionView, although below code works for a UITableView (having one section):

[NSIndexPath indexPathForRow:[yourArray count]-1 inSection:0] 

but not been able to achieve the same thing for a UICollectionView.

4 Answers

Swift 5

extension UICollectionView {

    func getLastIndexPath() -> IndexPath {
    
        let lastSectionIndex = max(0, self.numberOfSections - 1)
        let lastRowIndex = max(0, self.numberOfItems(inSection: lastSectionIndex) - 1)
    
        return IndexPath(row: lastRowIndex, section: lastSectionIndex)
    }
}
Related