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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5137943/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 19:02:57  来源:igfitidea点击:

How to know when UITableView did scroll to bottom in iPhone

iosuitableviewscroll

提问by Son Nguyen

I would like to know when a UITableViewdid scroll to bottom in order to load and show more content, something like a delegate or something else to let the controller know when the table did scroll to bottom.

我想知道何时UITableView滚动到底部以加载和显示更多内容,例如委托或其他让控制器知道表格何时滚动到底部的内容。

Does anyone know this, please help me, thanks in advance!

有谁知道这个,请帮帮我,先谢谢了!

采纳答案by Ajay

The best way is to test a point at the bottom of the screen and use this method call when ever the user scrolls (scrollViewDidScroll):

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

- (NSIndexPath *)indexPathForRowAtPoint:(CGPoint)point

Test a point near the bottom of the screen, and then using the indexPath it returns check if that indexPath is the last row then if it is, add rows.

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

回答by neoneye

in the tableview delegate do something like this

在 tableview 委托中做这样的事情

ObjC:

对象:

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

Swift:

迅速:

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    let offset = scrollView.contentOffset
    let bounds = scrollView.bounds
    let size = scrollView.contentSize
    let inset = scrollView.contentInset
    let y = offset.y + bounds.size.height - inset.bottom
    let h = size.height
    let reload_distance:CGFloat = 10.0
    if y > (h + reload_distance) {
        print("load more rows")
    }
}

回答by Eyeball

Modified neoneyes answer a bit.

修改过的新手回答一下。

This answer targets those of you who only wants the event to be triggered once per release of the finger.

这个答案针对那些只希望每次松开手指时触发一次事件的

Suitable when loading more content from some content provider (web service, core data etc). Note that this approach does not respect the response time from your web service.

适用于从某些内容提供商(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");
    }
}

回答by 8suhas

add this method in the UITableViewDelegate:

将此方法添加到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");
    }
}

回答by Iftach Orr

None of the answers above helped me, so I came up with this:

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

- (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
    }
}

回答by Wolfgang Schreurs

Use – tableView:willDisplayCell:forRowAtIndexPath:(UITableViewDelegatemethod)

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

Simply compare the indexPathwith the items in your data array (or whatever data source you use for your table view) to figure out if the last element is being displayed.

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

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

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

回答by Anomie

UITableViewis a subclass of UIScrollView, and UITableViewDelegateconforms to UIScrollViewDelegate. So the delegate you attach to the table view will get events such as scrollViewDidScroll:, and you can call methods such as contentOffseton the table view to find the scroll position.

UITableView是 的子类UIScrollView,并且UITableViewDelegate符合UIScrollViewDelegate。因此,您附加到表格视图的委托将获得诸如 之类的事件scrollViewDidScroll:,您可以调用诸如contentOffset表格视图之类的方法来查找滚动位置。

回答by user1641587

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];

Nice and simple

好看又简单

回答by Jay Mayu

in Swiftyou can do something like this. Following condition will be true every time you reach end of the tableview

Swift你可以做这样的事情。每次到达 tableview 末尾时,以下条件都为真

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

回答by footyapps27

Building on @Jay Mayu's answer, which I felt was one of the better solutions:

基于@Jay Mayu 的回答,我认为这是更好的解决方案之一:

Objective-C

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

Swift 2.x

斯威夫特 2.x

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