2011-05-16 77 views
30

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

+0

可能重复[\ [iPhone \]如何知道UITableView何时滚动到底部](http://stackoverflow.com/questions/5137943/iphone-how-to-know-when-uitableview-did - 滚动到底部) – 2011-05-16 21:45:14

+0

请在发布之前搜索。搜索你的标题可以发现我已经链接的这个问题,它与Henri给你的答案有相同的信息。 – 2011-05-16 22:50:39

+2

下面是使用Swift的无限UITableView演示:https://github.com/i-schuetz/tableview_infinite它使用异步任务加载数据并显示加载页脚。 – Ixx 2014-06-27 19:45:03

回答

38

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

编辑:放在一起抽象的代码,不知道它是否工作,但应该。

- (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]; 
    } 
} 
+0

介意显示一些码? – aherlambang 2011-05-16 21:43:10

+0

没有项目atm来快速显示内容。我认为你的viewController是tableView的委托。如果是这样,只需将scrollViewDidScroll:添加到viewController并将其用作触发更新方法的地方。将contentOffset.y与最大值(contenSize.y - tableView.frame.size.height)进行比较应该非常简单。 – 2011-05-16 21:45:23

+0

关键是要认识到UITableView是UIScrollView的子类,它也传递委托方法。 – 2011-05-16 21:46:33

0

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

+4

,因为我想调用一个REST Web服务来添加更多的行,因为用户可以看到更多的条目,而不是“加载更多文章”类的东西,为什么不自动执行 – aherlambang 2011-05-16 21:42:57

57

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

,而不是

- (void)scrollViewDidScroll: (UIScrollView)scroll 

我用

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

这是方便多了,导致事件只触发一次。 我在我的应用程序中使用此代码在底部的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"); 
    } 
} 

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

+0

这是我只需要打一个电话给我的服务器。 ' - (void)scrollViewDidScroll:'被触发多次以致呼叫你的服务器。除非你按照@Henri Normak的建议在他的评论中提出并添加更新检查。这很简单。谢谢@ user944351 – Ben 2013-11-26 03:46:13

+0

很好的解决方案。 +1谢谢 – 2014-01-03 18:59:27

+0

这是最好的解决方案。干净的代码,没有ivar – OMGPOP 2014-02-26 09:05:03

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

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

它可以更简单的方式来实现我猜,你只需要确定的滚动方向在这种情况下,当用户滚动向下它。

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

CGPoint pointNow; 

在.m文件,在scrollViewDidScroll方法,我们需要实现这个代码: -

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

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

对于一个路段的的tableView:

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

与多个部分:

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

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

+2

辉煌的答案,更喜欢此相比scrollViewDidScroll选项 - 感谢vikingosegundo! – Rob 2014-01-16 14:00:07

+0

如果使用加载或错误单元格,请确保有一个如下所示的小检查:if(indexPath.row> 5)或简单地将boolean noMoreDataAvailble设置为false。 – 2014-04-18 14:12:54

+0

如果您希望在用户进入最后一个单元格之前稍微加载数据,那就不太好了。当您在 – Tander 2015-04-30 11:01:10

1

斯威夫特

这里是@user944351's answer雨燕版本:

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

就在这个方法添加到您的UITableView委托类。我发现-40太大,但您可以根据自己的需要进行调整。

更多信息

  • 参见my fuller answer具有方法的示例重新加载使用用于数据库限制和偏移数据。
相关问题