【问题标题】:Pull To Refresh in iOS 7iOS 7 中的下拉刷新
【发布时间】:2013-11-23 16:12:00
【问题描述】:

我正在尝试让下拉刷新功能在我的表视图中的 iOS 7 上正常工作。在 viewDidLoad 上,我有:

self.refreshControl = [[UIRefreshControl alloc] init];

    [self.refreshControl addTarget:self action:@selector(refreshInvoked:forState:) forControlEvents:UIControlEventValueChanged];

然后我运行:

-(void) refreshInvoked:(id)sender forState:(UIControlState)state {
    // Refresh table here...
    [_allEntries removeAllObjects];
    [self.tableView reloadData];
    [self refresh];
}

当刷新方法调用的请求完成后,在 didCompleteRequest 代码中,我有:

[self.refreshControl endRefreshing];

在 iOS 6 上,这意味着当你在 table view 上下拉时,它会显示一个圆形箭头,当你拉动时会被拉长,拉得足够远后,它会刷新。不过,现在我看不到圆形箭头,只有一个 UIActivityIndi​​cator。它有时也会起作用,有时不会。我错过了什么?

【问题讨论】:

  • 只有一个活动指示器是 iOS 7 中 UIRefreshControl 的默认外观。您的代码中也缺少这一点:[self.myTableView addSubview:refreshControl];
  • 我看不出这对应用有什么影响。尝试使用和不使用[self.tableView addSubview:self.refreshControl]; 并没有看到任何变化。 @Nikos M.

标签: ios ios7 pull-to-refresh


【解决方案1】:

在你的 UITableView 中添加 UIRefreshControl...

1) 在 ViewDidLoad..

- (void)viewDidLoad
{
    [super viewDidLoad];

    //to add the UIRefreshControl to UIView
    UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
    refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:@"Please Wait..."]; //to give the attributedTitle
    [refreshControl addTarget:self action:@selector(refresh:) forControlEvents:UIControlEventValueChanged];
    [tblVideoView addSubview:refreshControl];
}

2)调用相关方法刷新UITableView数据...

- (void)refresh:(UIRefreshControl *)refreshControl
{
    [self refreshTableData]; //call function you want
    [refreshControl endRefreshing];
}

Swift 的 OR

 let refreshControl : UIRefreshControl = UIRefreshControl.init()
 refreshControl.attributedTitle = NSAttributedString.init(string: "Please Wait...")
 refreshControl.addTarget(self, action: #selector(refresh), forControlEvents: UIControlEvents.ValueChanged)
 feedTable.addSubview(refreshControl)

 func refresh(refreshControl:UIRefreshControl){
      self.refreshTableData()//call function you want
      refreshControl.endRefreshing()
 }

【讨论】:

    【解决方案2】:

    您所说的“UIActivityIndi​​cator”是 UIRefreshControl 的新默认外观。

    你往下拉,当圆圈完成时,它显示你离触发刷新有多近。

    【讨论】:

    • 谢谢,所以就我发布的代码而言,不需要更多了吗? (我意识到我遗漏了我为填充表格而创建的实际刷新方法)。
    • 不,你很高兴。你可以改变它的颜色,但不幸的是没有办法恢复到旧的柏忌式风格。
    • 你能告诉我我们如何消除他的混蛋或我们发布它时的小延迟吗?
    【解决方案3】:

    您可以移除/隐藏默认活动指示器,并添加您自己的图像和动画。

    还有一个特定的阈值(距离),在调用刷新之前表格必须被拉过。

    这是我们关于自定义拉取刷新控件的教程(在 Objective-c 和 swift 中):http://www.jackrabbitmobile.com/design/ios-custom-pull-to-refresh-control/

    希望对你有帮助,如果我能回答其他问题,请告诉我

    【讨论】:

      【解决方案4】:

      快速更新

      对于表格视图

       - (void)viewDidLoad
       {
          let refreshControl = UIRefreshControl()
          refreshControl.attributedTitle = NSAttributedString(string: "Please  Wait..")
          tableView.addSubview(refreshControl)
          refreshControl.addTarget(self, action: #selector(refreshTable), forControlEvents: UIControlEventValueChanged)
       }
      
      - (void)refreshTable {
          //Refresh Data here
          //......
          //Once all the data is fetched. If you are loading asynchronously add the below code inside the async block
          refreshControl.endRefreshing()
          [tableView reloadData];
       }
      

      对于 UITableViewController

      在 UITableViewController 中有一个名为 refreshControl 的默认属性,默认为 nil。如果您只想初始化 refreshControl 并分配它。

      let refreshControl = UIRefreshControl()
      refreshControl.attributedTitle = NSAttributedString(string: "Please Wait..")
      yourTableViewController.refreshControl = refreshControl
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-03-30
        • 2012-12-31
        • 2021-11-14
        • 2011-07-05
        • 2011-09-29
        • 1970-01-01
        • 2014-04-11
        相关资源
        最近更新 更多