Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a UITableView's visible sections?

UITableView provides the methods indexPathsForVisibleRows and visibleCells, but how can I get the visible sections?

like image 820
iXcoder Avatar asked Nov 16 '10 04:11

iXcoder


2 Answers

Or the really easy way would be to take advantage of valueForKeyPath and the NSSet class:

NSSet *visibleSections = [NSSet setWithArray:[[self.tableView indexPathsForVisibleRows] valueForKey:@"section"]];

Basically you get an array of the section values in the visible rows and then populate a set with this to remove duplicates.

like image 155
Christopher King Avatar answered Oct 14 '22 15:10

Christopher King


Swift version

if let visibleRows = tableView.indexPathsForVisibleRows {
    let visibleSections = visibleRows.map({$0.section})
}
like image 34
AAV Avatar answered Oct 14 '22 15:10

AAV