[ACCEPTED]-Getting visible cell from UITableView pagingEnabled-uitableview
Well, on the off chance that you never figured 7 out a solution, or for whoever comes to 6 this question next, I'll provide you with 5 the answer you were looking for. UITableView 4 will provide you with the indexPaths you 3 are looking for, and then UITableView will 2 happily provide you with the cells that 1 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.
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)!)
}
0
Rather than focusing on when the UITableView 3 requests a cell, you should be focusing 2 on when it displays the cell, which is indicated 1 by the delegate method tableView:willDisplayCell:forRowAtIndexPath
.
Simple and elegant way to retrieve visible 3 cells of UITableView
, no need to get visible cells 2 by using indexpath
values
NSArray * visibleCells = tableView.visibleCells;
NSLog(@"Total visible Cells: %i", [visibleCells count]);
If indexpath
's of visible cells 1 are needed
NSArray * paths = [tableView indexPathsForVisibleRows];
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])")
}
0
Get UITableView current visible cell index
let visibleRect = CGRect(origin: tableview.contentOffset, size: tableview.bounds.size)
let visiblePoint = CGPoint(x: visibleRect.midX, y: visibleRect.midY)
let visibleIndexPath = tableview.indexPathForItem(at: visiblePoint)
Get 1 current shown UITableView cell index
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
for cell in yourBableview.visibleCells {
let indexPath = yourBableview.indexPath(for: cell)
print(indexPath)
}
}
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.