ios 滚动到底部时向 UITableView 添加行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6023683/
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:56:29  来源:igfitidea点击:

Add rows to UITableView when scrolled to bottom

objective-ccocoa-touchiosuitableview

提问by aherlambang

Is there a way to trigger an event, such as with an IBAction, when a user scrolls to the bottom of a UITableView? I would like to add more rows if this happens. How do I do this?

IBAction当用户滚动到 a 的底部时,有没有办法触发事件,例如使用 an UITableView?如果发生这种情况,我想添加更多行。我该怎么做呢?

回答by user944351

Unless it′s a little late, but i think i found a better solution:

除非有点晚了,但我想我找到了一个更好的解决方案:

instead of

代替

 - (void)scrollViewDidScroll: (UIScrollView)scroll

i used

我用了

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate

This is much more convenient, cause the event is only triggered once. I used this code in my application to load more rows in my tableview at the bottom (maybe you recognize this type of reloading from the facebook application - only difference that they are updating at the top).

这更方便,因为事件只触发一次。我在我的应用程序中使用此代码在底部的 tableview 中加载更多行(也许您从 facebook 应用程序中识别出这种类型的重新加载 - 唯一的区别是它们在顶部更新)。

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {    
    NSInteger currentOffset = scrollView.contentOffset.y;
    NSInteger maximumOffset = scrollView.contentSize.height - scrollView.frame.size.height;

    if (maximumOffset - currentOffset <= -40) {
        NSLog(@"reload");
    }
}

Hope anyone will help this.

希望任何人都会对此有所帮助。

回答by Henri Normak

Simply listen to the scrollViewDidScroll:delegate method, compare the content offset with the current possible offset and if lower then some threshold call your method to update the tableview. Don't forget to call [tableView reloadData]to make sure it reload the newly added data.

只需聆听scrollViewDidScroll:委托方法,将内容偏移量与当前可能的偏移量进行比较,如果低于某个阈值,则调用您的方法来更新 tableview。不要忘记调用[tableView reloadData]以确保它重新加载新添加的数据。

EDIT: Put together abstract code, not sure if it works, but should.

编辑:将抽象代码放在一起,不确定它是否有效,但应该。

- (void)scrollViewDidScroll: (UIScrollView *)scroll {
     // UITableView only moves in one direction, y axis
     CGFloat currentOffset = scroll.contentOffset.y;
     CGFloat maximumOffset = scroll.contentSize.height - scroll.frame.size.height;

     // Change 10.0 to adjust the distance from bottom
     if (maximumOffset - currentOffset <= 10.0) {
          [self methodThatAddsDataAndReloadsTableView];
     }
}

回答by vikingosegundo

I use this snippet. in tableView:willDisplayCell:forRowAtIndexPath:I check, if the cell with the last index path is about to be diplayed.

我使用这个片段。在tableView:willDisplayCell:forRowAtIndexPath:我检查中,是否即将显示具有最后一个索引路径的单元格。

For a tableView with one section:

对于具有一个部分的 tableView:

[indexPath isEqual:[NSIndexPath indexPathForRow:[self tableView:self.tableView numberOfRowsInSection:0]-1 inSection:0]

with more sections:

更多部分:

[indexPath isEqual:[NSIndexPath indexPathForRow:[self tableView:self.tableView numberOfRowsInSection:0]-1 inSection:[self numberOfSectionsInTableView:self.tableView]-1]


-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(!_noMoreDataAvailable)
    {
        if ([indexPath isEqual:[NSIndexPath indexPathForRow:[self tableView:self.tableView numberOfRowsInSection:0]-1 inSection:0]])
        {
            [self.dataSourceController fetchNewData];
        }
    }
}

after fetching the dataSourceController will inform the tableView delegate and this will reload the data.

获取 dataSourceController 后将通知 tableView 委托,这将重新加载数据。

回答by Suragch

Swift

迅速

Here is the Swift version of @user944351's answer:

这是@user944351 答案的 Swift 版本:

func scrollViewDidEndDragging(scrollView: UIScrollView, willDecelerate decelerate: Bool) {

    let currentOffset = scrollView.contentOffset.y
    let maximumOffset = scrollView.contentSize.height - scrollView.frame.size.height

    if maximumOffset - currentOffset <= -40 {
        print("reload")
    }
}

Just add this method to your UITableViewdelegate class. I found -40too large but you can adjust it according to your needs.

只需将此方法添加到您的UITableView委托类即可。我发现-40太大了,但您可以根据需要进行调整。

More info

更多信息

  • See my fuller answerthat has an example of method to reload data using limits and offsets for the database.
  • 请参阅我的更完整的答案,其中包含使用数据库的限制和偏移量重新加载数据的方法示例。

回答by bi Park

NSInteger currentOffset = scrollView.contentOffset.y;
NSInteger maximumOffset = scrollView.contentSize.height - scrollView.frame.size.height;

// Hit Bottom?
if ((currentOffset > 0) && (maximumOffset - currentOffset) <= 10) {
   // Hit Bottom !! 
}

回答by Jas_meet

It can be achieved in much simpler way I guess, you just need to determine the scrolling direction as in this case it is when the user is scrolling downwards.

我想它可以通过更简单的方式实现,您只需要确定滚动方向,就像在这种情况下用户向下滚动时一样。

First in your .h file declare a variable :

首先在你的 .h 文件中声明一个变量:

CGPoint pointNow;

In .m file, in scrollViewDidScroll method we need to implement this code:-

在 .m 文件中,在 scrollViewDidScroll 方法中,我们需要实现以下代码:-

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView.contentOffset.y > pointNow.y) {
        //Enter code here
    }
}

回答by rekle

I'm not sure why you'd want to do this, but I imagine you could add some code to your cellForRowAtIndexPathmethod so that if indexPath.row == the last row, call the method to add more rows...

我不确定你为什么要这样做,但我想你可以在你的cellForRowAtIndexPath方法中添加一些代码,这样如果 indexPath.row == 最后一行,调用该方法添加更多行......