如何获得UITableView的可见矩形?

She*_*lam 11 iphone cocoa-touch objective-c uitableview uiscrollview

我试图检测用户是否已滚动到UITableView的底部,以便我可以做一些额外的事情.为了正确计算事物,我需要获得UITableView的可见rect.我怎样才能做到这一点?

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

}
Run Code Online (Sandbox Code Playgroud)

Zic*_*nic 22

UITableView只是一个UIScrollView子类,因此所有常用的UIScrollView方法都适用,例如UITableView的可见rect就是它的边界:

CGRect visibleRect = [myTableView bounds];
Run Code Online (Sandbox Code Playgroud)

visibleRect的来源只是contentOffset,因此您可以使用的另一种方法是:

CGFloat distanceFromBottom = [self.tableView contentSize].height - [self.tableView contentOffset].y;
Run Code Online (Sandbox Code Playgroud)


小智 3

您可以检查[tableView visibleCells]以获取可见单元格或检查tableView indexPathsForVisibleRows]并确定用户是否滚动到底部。