Skip to content

Instantly share code, notes, and snippets.

@igorleonovich
Last active August 6, 2023 11:10
Show Gist options
  • Save igorleonovich/11e9daaa4e90d3664f9c794a1e67a219 to your computer and use it in GitHub Desktop.
Save igorleonovich/11e9daaa4e90d3664f9c794a1e67a219 to your computer and use it in GitHub Desktop.
Returns indexPath of given ordered cell number for any section/rows configuration
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