Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableView with one visible cell: determine which is most visible

Given a UITableView with a single visible cell at any given time, how can I determine which cell is most in view while the table view is being scrolled?

I know I can get an array of visible cells by doing this:

NSArray *paths = [tableView indexPathsForVisibleRows];

And then get the last cell (or first, or whatever) by doing:

UITableViewCell* cell = (UITableViewCell*)[tableView cellForRowAtIndexPath:[paths lastObject]];

But how to I compare all the visible cells and determine which of them is most in view?

like image 421
brandonscript Avatar asked Dec 11 '14 03:12

brandonscript


1 Answers

The following logic would get you the most visible cell at the end of the scroll:

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {

    CGRect visibleRect = (CGRect){.origin = self.tableView.contentOffset, .size = self.tableView.bounds.size};
    CGPoint visiblePoint = CGPointMake(CGRectGetMidX(visibleRect), CGRectGetMidY(visibleRect));
    NSIndexPath *visibleIndexPath = [self.tableView indexPathForRowAtPoint:visiblePoint];

}
like image 120
Sebyddd Avatar answered Oct 05 '22 21:10

Sebyddd