UIRefreshControl:刷新时 UITableView '卡住'
Posted
技术标签:
【中文标题】UIRefreshControl:刷新时 UITableView \'卡住\'【英文标题】:UIRefreshControl: UITableView 'stuck' while refreshingUIRefreshControl:刷新时 UITableView '卡住' 【发布时间】:2012-11-14 21:10:25 【问题描述】:我在实现 UIRefreshControl 时遇到问题 - 当您下拉时,“blob”工作得非常好,刷新微调器工作正常,但 tableView 在刷新时不会向上滚动到微调器。相反,它会停留在原来的位置,直到刷新完成,此时它会返回到屏幕顶部
刷新的代码是:
- (void)viewDidLoad
self.refreshControl = [[UIRefreshControl alloc] init];
[self.refreshControl addTarget:self action:@selector(refreshView:)forControlEvents:UIControlEventValueChanged];
- (void)refreshView:(UIRefreshControl *)refresh
dispatch_async(dispatch_get_main_queue(), ^
(...code to get new data here...)
[self.refreshControl endRefreshing];
我发现如果没有 dispatch_async,即使是刷新微调器也不起作用 - 被拉下的位看起来只是白色
有没有人知道我做错了什么?我发现的所有实现示例似乎都与我正在做的事情相匹配,而且我在 API 文档中没有发现任何表明我遗漏了任何内容的内容
【问题讨论】:
但是我告诉过你要从主线程调用 UIKit! 【参考方案1】:你可以改为关注
- (void)refreshView:(UIRefreshControl *)refresh
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^
// (...code to get new data here...)
dispatch_async(dispatch_get_main_queue(), ^
//any UI refresh
[self.refreshControl endRefreshing];
);
);
-refreshView:将在主线程上调用,所有 UI 更新都使用主线程。因此,如果您将主线程用于“获取新数据的代码”,它将“卡住”
【讨论】:
这似乎工作得很好 - 如果 indexPath.row 不在我的数据数组中,我还必须通过使用旧数据来实现类似于 ***.com/questions/10605038/… 的修复 - 这对于 ios 开发人员来说是新的我相信我会及时理解为什么 :) 谢谢! GCD 开始时似乎很难。这是一个非常好的队列和线程框架,你会喜欢的。很高兴它成功了。以上是关于UIRefreshControl:刷新时 UITableView '卡住'的主要内容,如果未能解决你的问题,请参考以下文章
使用 UIRefreshControl 刷新 UITableView
带有 UIRefreshControl 的 ScrollView 在显示为模式表时不会刷新