IOS博客項目搭建-11-刷新數據UIRefreshControl


IOS自帶刷新數據的方法UIRefreshControl,該方法能夠下拉刷新獲取數據json


在加載view的時候,就調用刷新控件的方法來獲取新數據:
api

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // 0.集成刷新控件 =====【2016-04-12 新增刷新控件方法】======
    [self setupRefreshView];
    
    // 1.設置導航欄的內容
    [self setupNavBar];
    
    // 2.加載微博數據
    [self setupStatusData];
}


刷新控件的具體方法:數組

/**
 *  集成刷新控件【2016-04-12】
 */
- (void)setupRefreshView
{
    UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
    
    // 監聽刷新控件的狀態改變
    [refreshControl addTarget:self action:@selector(refreshControlStateChange:) forControlEvents:UIControlEventValueChanged];
    
    [self.tableView addSubview:refreshControl];
}


-(void)refreshControlStateChange:(UIRefreshControl*)refreshControl
{
    // 刷新數據(向新浪獲取更新的微博數據)
    
    // 1.建立請求管理對象
    AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
    
    // 2.封裝請求參數
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"access_token"] = [IWAccountTool account].access_token;
    params[@"count"] = @5;
    
    // 取出數組第一個元素
    IWStatusFrame *statusFrame = self.statusFrames[0];
    // 加載ID比since_id大的微博
    params[@"since_id"] = statusFrame.status.idstr;
    
    
    // 3.發送請求
    [mgr GET:@"https://api.weibo.com/2/statuses/home_timeline.json" parameters:params
     success:^(AFHTTPRequestOperation *operation, id responseObject) {
         // 將字典數組轉爲模型數組(裏面放的就是IWStatus模型)
         NSArray *statusArray = [IWStatus objectArrayWithKeyValuesArray:responseObject[@"statuses"]];
         // 建立frame模型對象
         NSMutableArray *statusFrameArray = [NSMutableArray array];
         for (IWStatus *status in statusArray) {
             //              if (status.pic_urls.count == 9) {
             //                  NSMutableArray *tempArray = [NSMutableArray array];
             //                  for (int i = 0; i<4; i++) {
             //                      [tempArray addObject:status.pic_urls[i]];
             //                  }
             //                  status.pic_urls = tempArray;
             //              }
             IWStatusFrame *statusFrame = [[IWStatusFrame alloc] init];
             // 傳遞微博模型數據
             statusFrame.status = status;
             [statusFrameArray addObject:statusFrame];
         }
         // 賦值
         // 將最新的數據追加到舊數據的最前面
         // 舊數據:self.statusFrames
         // 新數據:statusFrameArray
         NSMutableArray *tempArray = [NSMutableArray array];
         [tempArray addObjectsFromArray:statusFrameArray];
         [tempArray addObjectsFromArray:self.statusFrames];
         self.statusFrames = tempArray;
         
         
         // 刷新表格
         [self.tableView reloadData];
         
         // 讓刷新控件中止顯示菊花
         [refreshControl endRefreshing];
     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
         
         // 讓刷新控件中止顯示菊花
          [refreshControl endRefreshing];
     }];

}


以上的代碼即爲調用系統自帶的方法來刷新獲取數據。url

相關文章
相關標籤/搜索