Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get a UITableView's visible rect?

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!");
    }      

}
like image 290
Sheehan Alam Avatar asked Jul 21 '11 14:07

Sheehan Alam


People also ask

How do I scroll to top Tableview?

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.


1 Answers

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;
like image 51
Ziconic Avatar answered Sep 24 '22 04:09

Ziconic