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
UIRefreshControl - Pull To Refresh in iOS 7
提问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 UITableView
or UICollectionView
, it should work like a charm by itself. You do need to stop it though.
您不必显式设置 frame 或 start UIRefreshControl
。如果它是 UITableView
or 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 refreshTable
function, 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 endRefreshing
and reloadData
calls to your completion handler.
请注意,如果您异步刷新数据,则需要移动endRefreshing
并reloadData
调用完成处理程序。
回答by Ayush Goel
You forgot to attach the UIRefreshControl
to your table view.
您忘记将 附加UIRefreshControl
到您的表视图。
Change your viewDidLoad
to
改变你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
.