- (void)viewDidLoad { [super viewDidLoad]; // 集成刷新控件 [self setupRefresh]; } /** * 集成下拉刷新 */-(void)setupRefresh{ //1.添加刷新控件 UIRefreshControl *control=[[UIRefreshControl alloc]init]; [control addTarget:self action:@selector(refreshStateChange:) forControlEvents:UIControlEventValueChanged]; [self.tableView addSubview:control]; //2.马上进入刷新状态,并不会触发UIControlEventValueChanged事件 [control beginRefreshing]; // 3.加载数据 [self refreshStateChange:control];}
/** * UIRefreshControl进入刷新状态:加载最新的数据 */-(void)refreshStateChange:(UIRefreshControl *)control{ // 3.发送请求 AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager]; [mgr GET:@"https://api.weibo.com/2/statuses/public_timeline.json" parameters:nil success:^(AFHTTPRequestOperation *operation, NSDictionary *responseObject){ //1.获取数据,处理数据,传递数据给tableView,如: // 将最新的微博数据,添加到总数组的最前面// NSRange range = NSMakeRange(0, newStatuses.count);// NSIndexSet *set = [NSIndexSet indexSetWithIndexesInRange:range];// [self.statuses insertObjects:newStatuses atIndexes:set]; //2.刷新表格 [self.tableView reloadData]; // 3. 结束刷新 [control endRefreshing]; } failure:^(AFHTTPRequestOperation *operation, NSError *error) { // 结束刷新刷新 ,为了避免网络加载失败,一直显示刷新状态的错误 [control endRefreshing]; }];}