I have a UITableView with pagingEnabled. Each cell takes up the viewing area of the table. Meaning, each cell is the same height and width as the table. I\'m using custom
Rather than focusing on when the UITableView requests a cell, you should be focusing on when it displays the cell, which is indicated by the delegate method tableView:willDisplayCell:forRowAtIndexPath
.
Swift 4+
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
let firstVisibleIndexPath = self.tableView.indexPathsForVisibleRows?[0]
print("top visible cell section is \([firstVisibleIndexPath!.section])")
}
Converted Douglas example to Swift:
let tableView = self.tableView // Or however you get your table view
let paths = tableView.indexPathsForVisibleRows
// For getting the cells themselves
let visibleCells : NSMutableSet = []
for path in paths! {
visibleCells.addObject(tableView.cellForRowAtIndexPath(path)!)
}
Simple and elegant way to retrieve visible cells of UITableView
, no need to get visible cells by using indexpath
values
NSArray * visibleCells = tableView.visibleCells;
NSLog(@"Total visible Cells: %i", [visibleCells count]);
If indexpath
's of visible cells are needed
NSArray * paths = [tableView indexPathsForVisibleRows];
Well, on the off chance that you never figured out a solution, or for whoever comes to this question next, I'll provide you with the answer you were looking for. UITableView will provide you with the indexPaths you are looking for, and then UITableView will happily provide you with the cells that match those index paths:
UITableView *tableView = self.tableView; // Or however you get your table view
NSArray *paths = [tableView indexPathsForVisibleRows];
// For getting the cells themselves
NSMutableSet *visibleCells = [[NSMutableSet alloc] init];
for (NSIndexPath *path in paths) {
[visibleCells addObject:[tableView cellForRowAtIndexPath:path]];
}
// Now visibleCells contains all of the cells you care about.