UITableView provides the methods indexPathsForVisibleRows and visibleCells, but how can I get the visible sections?
Swift 4 and Swift 5
extension UITableView {
func returnVisibleCellsIndexes() -> [IndexPath] {
var indexes = [IndexPath]()
for cell in self.visibleCells {
if let indexpath = self.returnIndexPath(cell: cell) {
indexes.append(indexpath)
}
}
return indexes
}
func returnIndexPath(cell: UITableViewCell) -> IndexPath?{
guard let indexPath = self.indexPath(for: cell) else {
return nil
}
return indexPath
}
func returnVisibleCells() -> [UITableViewCell]{
let visiblecell = self.visibleCells
return visiblecell
}
}
How to use
let visibleCellsIndexes = self.tableView.returnVisibleCellsIndexes()
visibleCellsIndexes.forEach { (indexpath) in
print(indexpath.section)
}