IT虾米网

UIRefreshControl 下拉刷新

shasha 2018年06月24日 手机开发 1058 0
- (void)viewDidLoad { 
    [super viewDidLoad]; 
     
    //UIRefreshControl 
    //系统自带,继承自UIControl,UIControl继承自UIView 
    UIRefreshControl * refresh = [[UIRefreshControl alloc] init]; 
    [self.tableView addSubview:refresh]; 
     
    //设置标题 
    NSAttributedString * title = [[NSAttributedString alloc] initWithString:@"下拉刷新"]; 
    refresh.attributedTitle = title; 
     
    //监听什么时候开始下拉刷新 
    [refresh addTarget:self action:@selector(refresh:) forControlEvents:UIControlEventValueChanged]; 
 
 
} 
 
 
//监听方法 
- (void)refresh:(UIRefreshControl *)refresh 
{ 
    NSLog(@"开始刷新"); 
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
        
        [NSThread sleepForTimeInterval:2]; 
        //回主线程刷新 
         dispatch_sync(dispatch_get_main_queue(), ^{ 
              
             [refresh endRefreshing]; 
         }); 
    }); 
} 

评论关闭
IT虾米网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!