如何知道UITableView何时在iPhone中滚动到底部

Son*_*yen 97 scroll uitableview ios

我想知道什么时候UITableView滚动到底部以加载和显示更多内容,像委托或其他东西让控制器知道何时表滚动到底部.

有谁知道这个,请帮助我,提前谢谢!

neo*_*eye 245

在tableview委托中做这样的事情

- (void)scrollViewDidScroll:(UIScrollView *)aScrollView {
    CGPoint offset = aScrollView.contentOffset;
    CGRect bounds = aScrollView.bounds;
    CGSize size = aScrollView.contentSize;
    UIEdgeInsets inset = aScrollView.contentInset;
    float y = offset.y + bounds.size.height - inset.bottom;
    float h = size.height;
    // NSLog(@"offset: %f", offset.y);   
    // NSLog(@"content.height: %f", size.height);   
    // NSLog(@"bounds.height: %f", bounds.size.height);   
    // NSLog(@"inset.top: %f", inset.top);   
    // NSLog(@"inset.bottom: %f", inset.bottom);   
    // NSLog(@"pos: %f of %f", y, h);

    float reload_distance = 10;
    if(y > h + reload_distance) {
        NSLog(@"load more rows");
    }
}
Run Code Online (Sandbox Code Playgroud)

  • "scrollViewDidScroll"中的代码一直在用户在屏幕上向上/向下移动手指时执行.最好在"scrollViewDidEndDecelerating"下移动它.这样,当用户停止移动他的手指时,它将被执行一次. (4认同)
  • 非常有用的帖子.干杯. (2认同)

Eye*_*all 62

修改过的neoneyes回答了一下.

这个答案针对的是那些只希望每次释放手指时触发一次事件的.

适用于从某些内容提供商(Web服务,核心数据等)加载更多内容.请注意,此方法不尊重Web服务的响应时间.

- (void)scrollViewDidEndDragging:(UIScrollView *)aScrollView
                  willDecelerate:(BOOL)decelerate
{
    CGPoint offset = aScrollView.contentOffset;
    CGRect bounds = aScrollView.bounds;
    CGSize size = aScrollView.contentSize;
    UIEdgeInsets inset = aScrollView.contentInset;
    float y = offset.y + bounds.size.height - inset.bottom;
    float h = size.height;

    float reload_distance = 50;
    if(y > h + reload_distance) {
        NSLog(@"load more rows");
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 很棒的工作@eyeball ..你的答案是完美的..至少在我的观点. (4认同)
  • Dean,问题是知道桌面视图何时滚动到底部.你是从API获取数据还是不相关,不是吗? (2认同)

8su*_*has 38

在以下位置添加此方法UITableViewDelegate:

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{   
    CGFloat height = scrollView.frame.size.height;

    CGFloat contentYoffset = scrollView.contentOffset.y;

    CGFloat distanceFromBottom = scrollView.contentSize.height - contentYoffset;

    if(distanceFromBottom < height) 
    {
        NSLog(@"end of the table");
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 除了一个小细节,我喜欢这个解决方案.if(distanceFromBottom <= height) (3认同)

小智 19

上面的答案都没有帮助我,所以我想出了这个:

- (void)scrollViewDidEndDecelerating:(UIScrollView *)aScrollView
{   
    NSArray *visibleRows = [self.tableView visibleCells];
    UITableViewCell *lastVisibleCell = [visibleRows lastObject];
    NSIndexPath *path = [self.tableView indexPathForCell:lastVisibleCell];
    if(path.section == lastSection && path.row == lastRow)
    {
        // Do something here
    }
}
Run Code Online (Sandbox Code Playgroud)


Wol*_*urs 19

使用– tableView:willDisplayCell:forRowAtIndexPath:(UITableViewDelegate方法)

只需将indexPath数据与数据数组中的项(或您用于表视图的任何数据源)进行比较,即可确定是否显示了最后一个元素.

文档:http://developer.apple.com/library/ios/documentation/uikit/reference/UITableViewDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intfm/UITableViewDelegate/tableView : willDisplayCell: forRowAtIndexPath:


Aja*_*jay 17

最好的方法是测试屏幕底部的一个点,并在用户滚动(scrollViewDidScroll)时使用此方法调用:

- (NSIndexPath *)indexPathForRowAtPoint:(CGPoint)point
Run Code Online (Sandbox Code Playgroud)

测试屏幕底部附近的一个点,然后使用indexPath返回检查indexPath是否是最后一行,如果是,则添加行.


Ano*_*mie 13

UITableViewUIScrollView,并且UITableViewDelegate符合的子类UIScrollViewDelegate.因此,您附加到表视图的委托将获取诸如的事件scrollViewDidScroll:,您可以调用contentOffset表视图等方法来查找滚动位置.


小智 8

NSLog(@"%f / %f",tableView.contentOffset.y, tableView.contentSize.height - tableView.frame.size.height);

if (tableView.contentOffset.y == tableView.contentSize.height - tableView.frame.size.height)
        [self doSomething];
Run Code Online (Sandbox Code Playgroud)

很好,很简单


Jay*_*ayu 8

Swift你可以做这样的事情.每次到达tableview结束时,以下条件都将成立

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        if indexPath.row+1 == postArray.count {
            println("came to last row")
        }
}
Run Code Online (Sandbox Code Playgroud)


foo*_*s27 7

以@Jay Mayu的答案为基础,我觉得这是一个更好的解决方案:

Objective-C的

// UITableViewDelegate
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {

// Need to call the service & update the array
if(indexPath.row + 1 == self.sourceArray.count) {
    DLog(@"Displayed the last row!");
  }
}
Run Code Online (Sandbox Code Playgroud)

Swift 2.x

// UITableViewDelegate
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
    if (indexPath.row + 1) == sourceArray.count {
        print("Displayed the last row!")
    }
}
Run Code Online (Sandbox Code Playgroud)


Sha*_*yaz 5

当最后一个单元格开始显示时,我通常使用它来加载更多数据

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   if (indexPath.row ==  myDataArray.count-1) {
        NSLog(@"load more");
    }
}
Run Code Online (Sandbox Code Playgroud)


sam*_*ize 5

采取neoneye优秀的答案,但迅速,并重命名变量..

基本上我们知道如果yOffsetAtBottom超出表格内容高度我们已到达表格视图的底部.

func isTableViewScrolledToBottom() -> Bool {
    let tableHeight = tableView.bounds.size.height
    let contentHeight = tableView.contentSize.height
    let insetHeight = tableView.contentInset.bottom

    let yOffset = tableView.contentOffset.y
    let yOffsetAtBottom = yOffset + tableHeight - insetHeight

    return yOffsetAtBottom > contentHeight
}
Run Code Online (Sandbox Code Playgroud)


小智 5

这是swift 3.0版本代码.

   func scrollViewDidScroll(_ scrollView: UIScrollView) {

        let offset = scrollView.contentOffset
        let bounds = scrollView.bounds
        let size = scrollView.contentSize
        let inset = scrollView.contentInset
        let y: Float = Float(offset.y) + Float(bounds.size.height) + Float(inset.bottom)
        let height: Float = Float(size.height)
        let distance: Float = 10

        if y > height + distance {
            // load more data
        }
    }
Run Code Online (Sandbox Code Playgroud)