UIRefreshControl - 在 iOS 7 中下拉刷新

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

UIRefreshControl - Pull To Refresh in iOS 7

iosobjective-cuitableview

提问by Nivesh666

I'm trying to get the pull to refresh feature on iOS 7 in my Table View. In my viewDidLoad, I have:

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

refreshControl = [[UIRefreshControl alloc] init];
[self.mytableView setContentOffset:CGPointMake(0, refreshControl.frame.size.height) animated:YES];
[refreshControl beginRefreshing];
[refreshControl addTarget:self action:@selector(refreshTable) forControlEvents:UIControlEventValueChanged];

I then run:

然后我运行:

-(void)refreshTable {
    [self.mytableView reloadData];
    [refreshControl endRefreshing];
}

On iOS 6, this would mean that as you pull down on the table view, it would show the circular arrow that would get stretched out as you pull, and after pulled far enough, it would refresh. Right now, I see no circular arrow. What am I missing?

在 iOS 6 上,这意味着当你下拉 table view 时,它会显示圆形箭头,当你拉的时候会被拉长,拉得足够远后,它会刷新。现在,我看不到圆形箭头。我错过了什么?

回答by Yas T.

You do not have to explicitly set frame or start UIRefreshControl. If it is a UITableViewor UICollectionView, it should work like a charm by itself. You do need to stop it though.

您不必显式设置 frame 或 start UIRefreshControl。如果它是 UITableViewor UICollectionView,它本身应该像魅力一样工作。不过你确实需要阻止它。

Here is how you code should look like:

您的代码如下所示:

- (void)viewDidLoad {
    [super viewDidLoad];
    refreshControl = [[UIRefreshControl alloc]init];
    [refreshControl addTarget:self action:@selector(refreshTable) forControlEvents:UIControlEventValueChanged];

    if (@available(iOS 10.0, *)) {
        self.mytableView.refreshControl = refreshControl;
    } else {
        [self.mytableView addSubview:refreshControl];
    }
}

In your refreshTablefunction, you need to stop it when you are done refreshing your data. Here is how it is going to look like:

在您的refreshTable函数中,您需要在完成刷新数据后停止它。这是它的样子:

- (void)refreshTable {
    //TODO: refresh your data
    [refreshControl endRefreshing];
    [self.mytableView reloadData];
}

Please note that if you are refreshing your data asynchronously then you need to move endRefreshingand reloadDatacalls to your completion handler.

请注意,如果您异步刷新数据,则需要移动endRefreshingreloadData调用完成处理程序。

回答by Ayush Goel

You forgot to attach the UIRefreshControlto your table view.

您忘记将 附加UIRefreshControl到您的表视图。

Change your viewDidLoadto

改变你viewDidLoad

  refreshControl = [[UIRefreshControl alloc]init];
  [refreshControl addTarget:self action:@selector(refreshTable) forControlEvents:UIControlEventValueChanged];
  [self setRefreshControl:refreshControl];

P.S. Your view controller should be a subclass of UITableViewController.

PS 你的视图控制器应该是UITableViewController.