Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableView indexPathsForVisibleRows returns wrong rows

In my iOS 7 app, I have my view set to "extend edges under top bars" so my uitableview has the translucent effect when you scroll it under the navigation bar as you are scrolling down. But when you scroll back to the top, or when it loads, it properly positions the first cell below the nav bar, not underneath it.

The problem is, that indexPathsForVisibleRows sees the cell underneath the nav bar. So for example, if we scroll down so that all we see is cell index 1 (the 2nd cell), cell index 0 is underneath the nav bar and when we call indexPathsForVisibleRows, it returns index 0 instead of index 1 as the lowest cell that is visible.

Any other way around this?

like image 796
Jesse Avatar asked Dec 20 '22 20:12

Jesse


1 Answers

One option is to get the y-coordinate of the bottom of the navbar and translate this to the coordinate system of the tableview. Once you have that, you can get the indexPath or cell from the tableview based on that point.

UINavigationBar *navbar = self.navigationController.navigationBar;
CGRect localFrame = [self.tableView convertRect:navbar.bounds fromView:navbar];
CGPoint point = CGPointMake(0, localFrame.origin.y + localFrame.size.height + 1);
NSIndexPath *path = [self.tableView indexPathForRowAtPoint:point];
like image 64
rmaddy Avatar answered Jan 19 '23 12:01

rmaddy