StackOverflower StackOverflower - 1 year ago 65
iOS Question

Is it possible to get the "global" index from NSIndexPath?

If I have different sections on my

will give me the index within current section.

Is it possible to get the absolute index of the cell in the table without taking into account sections?

For example: if a table has 2 sections with the first having with 10 items, I want to get index = 10 for the first element of the second section.

Answer Source

I agree with some of the comments that this might not be the best way to implement infinite scrolling. Answering your original question though, you could do something like this in Swift:

extension UITableView {
    func globalIndexPath(for localIndexPath: NSIndexPath) -> Int {
        var totalRows = 0
        for i in 0..<localIndexPath.section {
            totalRows += self.numberOfRowsInSection(i)
        return totalRows + localIndexPath.row