ios 下拉刷新

- (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];
    }];
}
相關文章
相關標籤/搜索