Last active
August 6, 2023 11:10
-
-
Save igorleonovich/11e9daaa4e90d3664f9c794a1e67a219 to your computer and use it in GitHub Desktop.
Returns indexPath of given ordered cell number for any section/rows configuration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
extension UITableView { | |
func indexPath(for cellNumber: Int) -> IndexPath { | |
// Returns indexPath of given ordered cell number for any section/rows configuration | |
var cellIndex = 0 | |
var sectionIndex = 0 | |
var rowIndex = 0 | |
while sectionIndex < numberOfSections, cellIndex < cellNumber - 1 { | |
rowIndex = 0 | |
let rowsInSection = numberOfRows(inSection: sectionIndex) | |
while cellIndex < cellNumber - 1, rowIndex < rowsInSection - 1 { | |
cellIndex += 1 | |
rowIndex += 1 | |
} | |
sectionIndex += 1 | |
} | |
return IndexPath(row: rowIndex, section: sectionIndex - 1) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment