接上一个话题,实现了TabBar的点击刷新之后,开始继续写完成功能,刷新UITableView,因而考虑到iOS 10
之后,UIScrollView
已经有UIRefreshControl
的属性了,干脆用自带的写。因而就有了以下的代码:bash
UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
refreshControl.tintColor = [UIColor grayColor];
refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:@"下拉刷新"];
[refreshControl addTarget:self action:@selector(refreshTabView) forControlEvents:UIControlEventValueChanged];
self.newsTableView.refreshControl = refreshControl;
复制代码
-(void)refreshTabView
{
//添加一条数据
[self.newsData insertObject:[self.newsData firstObject] atIndex:0];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self.newsTableView reloadData];
if ([self.newsTableView.refreshControl isRefreshing]) {
[self.newsTableView.refreshControl endRefreshing];
}
});
}
复制代码
-(void)doubleClickTab:(NSNotification *)notification{
//这里有个坑 就是直接用NSInteger接收会有问题 数字不对
//由于上个界面传过来的时候封装成了对象,因此用NSNumber接收后再取值
NSNumber *index = notification.object;
if ([index intValue] == 1) {
//刷新
[self.newsTableView.refreshControl beginRefreshing];
}
}
复制代码
此时的效果以下,直接下拉刷新能够,可是点击TabBar不能够:ui
通过Google帮助,终于知道缘由,由于系统自带的UIRefreshControl有两个陷阱:spa
-beginRefreshing
方法不会触发UIControlEventValueChanged
事件;-beginRefreshing
方法不会自动显示进度圈。也就是说,只是调用-beginRefreshing
方法是无论用的,那么对应的须要作两件事:3d
只须要修改上面第3步中的代码以下:code
-(void)doubleClickTab:(NSNotification *)notification{
//这里有个坑 就是直接用NSInteger接收会有问题 数字不对
//由于上个界面传过来的时候封装成了对象,因此用NSNumber接收后再取值
NSNumber *index = notification.object;
if ([index intValue] == 1) {
//刷新
//animated不要为YES,不然菊花会卡死
[self.newsTableView setContentOffset:CGPointMake(0, self.newsTableView.contentOffset.y - self.newsTableView.refreshControl.frame.size.height) animated:NO];
[self.newsTableView.refreshControl beginRefreshing];
[self.newsTableView.refreshControl sendActionsForControlEvents:UIControlEventValueChanged];
}
}
复制代码
最终效果: cdn