【问题标题】:Problem detecting if UITableView has scrolled to the bottom检测 UITableView 是否滚动到底部的问题
【发布时间】:2011-10-15 01:01:39
【问题描述】:

我正在使用以下代码来检测我是否已到达 UITableView 的底部

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{ 
    if(self.tableView.contentOffset.y >= (self.tableView.contentSize.height - self.tableView.bounds.size.height)) {
        NSLog(@"bottom!");
        NSLog(@"%@", [self getLastMessageID]);
        [self getMoreStuff:[self getLastMessageID]];
    }

}

这很好用,但唯一的问题是当用户将 tableview 拉下(如拉刷新)时,代码会触发。我该如何处理?

【问题讨论】:

    标签: iphone objective-c cocoa-touch uitableview uiscrollview


    【解决方案1】:

    将滚动视图的最后一个位置存储在您的 didScroll 方法中。如果您检测到向下滚动并且最后一个位置已经是滚动视图的底部,那么您将忽略它。

    【讨论】:

      【解决方案2】:

      尝试使用另一种算法来检测您是否到达了表格视图的底部。例如,查看当前单元格的 indexPath 的 cellForRowAtIndexPath:(NSIndexPath *)indexPath,如果 IndexPath 中的行数等于您的数据数组中的行数,那么您到达了底部。

      【讨论】:

        【解决方案3】:

        试试这个方法

            if(self.tableview.contentOffset.y<0){
                      //it means table view is pulled down like refresh
                      return;
                    }
        else if(self.tableView.contentOffset.y >= (self.tableView.contentSize.height - self.tableView.bounds.size.height)) {
                NSLog(@"bottom!");
                NSLog(@"%@", [self getLastMessageID]);
                [self getMoreStuff:[self getLastMessageID]];
            }
        

        【讨论】:

        • 别忘了标签栏:(
        猜你喜欢
        • 1970-01-01
        • 2018-10-25
        • 1970-01-01
        • 2021-09-22
        • 2020-11-14
        • 1970-01-01
        • 1970-01-01
        • 2012-04-16
        • 2012-09-25
        相关资源
        最近更新 更多