【问题标题】:UIRefreshControl at the bottom of the UITableView iOS6?UITableView iOS6底部的UIRefreshControl?
【发布时间】:2013-02-03 05:55:54
【问题描述】:

是否可以在UITableView 的底部添加UIRefreshControl? 我会用它来加载更多数据。 请问有什么建议吗?

【问题讨论】:

    标签: uitableview ios6 uirefreshcontrol


    【解决方案1】:

    我相信这个问题不会有任何简单的解决方案。可能有人可以编写一个单独的库来实现此行为,而且一旦您在 tableview 中缓存数据,它将导致更多的复杂性。

    但是让我们分解问题,这可能会帮助您实现您想要的。我已使用以下代码在应用程序中添加更多行:

    - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
    {
        float endScrolling = scrollView.contentOffset.y + scrollView.frame.size.height;
        if (endScrolling >= scrollView.contentSize.height)
        {
            NSLog(@"Scroll End Called");
            [self fetchMoreEntries];
        }
    }
    

    或者你可以这样做:

    - (void)scrollViewDidScroll:(UIScrollView *)scrollView
    {
        if ((scrollView.contentOffset.y + scrollView.frame.size.height) >= scrollView.contentSize.height)
        {
            if (!self.isMoreData)
            {
                self.MoreData = YES;
    
                // call some method that handles more rows
            }
        }
    }
    

    您一定已经在我上面描述的许多问题中看到过这样的方法,当然不是您所要求的。但是您可以做的是,在上述代码加载更多数据的过程中,您可以添加一个子视图并显示一些类似于 UIRefreshControl 提供的图像。在子视图中添加图像以显示为进度,直到执行上述代码。回家这有帮助。

    顺便说一句,我建议你不要这样做,因为它只会浪费你的时间来制作如此小的东西,除非你只是为了学习目的而这样做。

    【讨论】:

      【解决方案2】:

      以下答案在 Xcode 8 和 Swift 3 中。

      先声明

      var spinner = UIActivityIndicatorView()
      

      比在viewDidLoad方法中写如下代码:

      spinner = UIActivityIndicatorView(activityIndicatorStyle: .whiteLarge)
      spinner.stopAnimating()
      spinner.hidesWhenStopped = true
      spinner.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: 60)
      tableView.tableFooterView = spinner
      

      现在终于override scrollViewDidEndDragging 委托方法具有以下内容:

      override func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
              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 reloadDistance = CGFloat(30.0)
              if y > h + reloadDistance {
                  print("fetch more data")
                  spinner.startAnimating()
              }
      }
      

      【讨论】:

      • 这是今年以来最正确的答案。我一直在使用 CCBottomRefreshControl,直到我不得不将我的项目转换为 swift 3.0 并且它开始到处崩溃。这是一个更好的解决方案。谢谢!
      • 伙计,你拯救了我的一天。这是swift 3的最佳解决方案
      • 没错,这是最好的解决方案
      • 好答案!一个小调整:初始化 spinner = UIActivityIndi​​catorView(),然后在 viewDidLoad() 中使用 spinner = UIActivityIndi​​catorView(activityIndi​​catorStyle: .whiteLarge) 再次初始化它。你可以消除其中之一。
      • 非常好的解决方案。请注意,在最新的 Swift 版本中,不需要“覆盖”语句。
      【解决方案3】:

      您可以使用 UIView 在底部自定义您的 refreshControl。创建 UIView 并将其作为 footerView 添加到 UITableView。

      UIView* footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
      
      [footerView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"refreshImage.png"]]];
      
      tableView.tableFooterView = footerView;
      

      隐藏它:tableView.tableFooterView.hidden = YES;

      实现 UIScrollView 委托 -

      (void)scrollViewDidScroll:(UIScrollView *)scrollView
      {
          if ((scrollView.contentOffset.y + scrollView.frame.size.height) >= scrollView.contentSize.height)
          {
                   tableView.tableFooterView.hidden = NO;
                  // call method to add data to tableView
          }
      
      }
      

      在向 tableView 添加数据之前,将当前偏移量保存为CGPoint offset = tableView.contentOffset;

      调用 reloadData 然后将之前保存的偏移设置回 tableView [tableView setContentOffset:offset animated:NO];

      这样你就可以感觉到底部添加了新数据。

      【讨论】:

      • 我在我的 iOS 7 应用程序上使用它,它似乎可以工作。我实际上将我的tableFooterView 设置为UIRefreshControl 视图
      • 抱歉,UIActivityIndicatorView 不是 UIRefreshControl
      【解决方案4】:

      我最近遇到了同样的问题,并为 UIScrollView 类编写了一个类别。这里是CCBottomRefreshControl

      【讨论】:

      • 有 ReactiveCocoa 依赖,所以没用。
      • 非常适合我。所有使用 UIScrollViewDelegate 方法的方法在性能方面都令人头疼。此方法使用 Default UIRefreshControl 元素,这比我遇到的所有其他方法都好得多。
      • 但是我遇到了一个问题。加载数据后,我无法删除 UIRefreshcontroller。 [refreshController endRefreshing] 什么都不做。
      • @ShreeshGarg 请查看新版本 0.4.1。修复了错误。
      • 嘿morbo,你能告诉我们如何降低刷新功能所需的尺寸高度吗?目前用户需要大量滚动表格来刷新数据。
      【解决方案5】:

      实际上,免费的 Sensible TableView 框架确实提供了开箱即用的功能。您指定批号,它将自动获取数据,将最后一个单元格显示为“加载更多”单元格。单击该单元格后,它将自动加载下一批,依此类推。值得一看。

      【讨论】:

      • 请声明您与此框架(如果有)的关系,这看起来像是垃圾邮件。
      • 没有任何关联!!只是一个超级粉丝!
      • 我知道了——但其他人可能没有。
      【解决方案6】:

      对于 UITableView,我建议采用以下方法:

      -(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
      {    
          // if this is the last row that we have in local data
          // and we didn't reach the total count of rows from the server side
          if (count == indexPath.row+1 && count < total)
          {
              // .. fetch more rows and reload table when data comes
          }
      }
      

      我没有故意使用滚动视图方法,因为滚动视图很慢。碰巧滚动视图滚动,我们请求更多数据,刷新更多行,滚动视图尚未完成滚动,它仍在减速并导致获取更多数据时出现问题。

      【讨论】:

      • 请简单解释@Prcela
      【解决方案7】:

      这个 Swift 库添加拉动刷新到UITableview 的底部。

      https://github.com/marwendoukh/PullUpToRefresh-iOS

      希望对你有所帮助。

      【讨论】:

        【解决方案8】:

        在 cocoapods 中使用 CCBottomRefreshControl,

        因为它是语言目标。 您可以将 pod 文件添加到您的 swift 项目中,然后运行它, 我已经做到了,对我来说没有问题

        希望对你有帮助。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-09-22
          • 1970-01-01
          • 2013-09-25
          • 1970-01-01
          • 2015-07-30
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多