目标 c - 滚动到底部时向 UITableView 添加行



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

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

而不是

 - (void)scrollViewDidScroll: (UIScrollView)scroll

我用了

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

这更方便,因为事件只触发一次。我在我的应用程序中使用此代码在底部的表视图中加载更多行(也许您认识到从 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");
    }
}

希望有人能对此有所帮助。

只需侦听 scrollViewDidScroll: 委托方法,将内容偏移量与当前可能的偏移量进行比较,如果低于某个阈值,则调用您的方法来更新表视图。不要忘记调用[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];
     }
}

我使用此代码片段。 tableView:willDisplayCell:forRowAtIndexPath:我检查具有最后一个索引路径的单元格是否即将被播放。

对于表具有一个部分的视图:

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

获取数据后,数据源控制器将通知表视图委托,这将重新加载数据。

NSInteger currentOffset = scrollView.contentOffset.y;
NSInteger maximumOffset = scrollView.contentSize.height - scrollView.frame.size.height;
// Hit Bottom?
if ((currentOffset > 0) && (maximumOffset - currentOffset) <= 10) {
   // Hit Bottom !! 
}
我想可以用

更简单的方式实现,您只需要确定滚动方向,因为在这种情况下,当用户向下滚动时。

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

CGPoint pointNow;

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

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

Swift

以下是 @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")
    }
}

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

更多信息

  • 请参阅我更完整的答案,其中包含使用数据库的限制和偏移量重新加载数据的方法示例。

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

最新更新