I am trying to detect if the user has scrolled to the bottom of a UITableView so that I can do some additional stuff. In order to calculate things properly, I need to get the UITableView's visible rect. How can I achieve this?
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
[_refreshHeaderView egoRefreshScrollViewDidScroll:scrollView];
int currentMaxPosition = CGRectGetMaxY([self.tableView visibleRect]);
int currentMinPosition = CGRectGetMinY([self.tableView visibleRect]);
int tableViewBottom = [self.tableView bounds].size.height - 100;
int tableViewTop = 0;
//get older messages once we're near the bottom
if (currentMaxPosition > tableViewBottom - 100)
{
NSLog(@"WE AT THE BOTTOM!");
}
}
To scroll to the top of our tableview we need to create a new IndexPath . This index path has two arguments, row and section . All we want to do is scroll to the top of the table view, therefore we pass 0 for the row argument and 0 for the section argument. UITableView has the scrollToRow method built in.
A UITableView is just a UIScrollView subclass, so all the usual UIScrollView methods apply, e.g. the visible rect of a UITableView is simply its bounds:
CGRect visibleRect = [myTableView bounds];
The origin of the visibleRect is simply the contentOffset, so another approach you can use is:
CGFloat distanceFromBottom = [self.tableView contentSize].height - [self.tableView contentOffset].y;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With