码迷,mamicode.com
首页 > 其他好文 > 详细

新浪微博客户端(18)-集成下拉刷新控件UIRefreshControl

时间:2016-11-03 00:00:15      阅读:391      评论:0      收藏:0      [点我收藏+]

标签:nbsp   ogr   sar   failure   stat   val   selector   request   控件   

 

HomeViewController.m

- (void)setupPullToRefreshView {

    UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
    [refreshControl addTarget:self action:@selector(refreshNewData:) forControlEvents:UIControlEventValueChanged];
    [self.tableView addSubview:refreshControl];

}


#pragma mark - UIRefreshContorl 监听方法
- (void)refreshNewData:(UIRefreshControl *)control {

    AFHTTPSessionManager *requestManager = [AFHTTPSessionManager manager];
    
    NSString *urlString = @"https://api.weibo.com/2/statuses/friends_timeline.json";
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"access_token"] = [DJAccountTool account].access_token;
    DJStatus *status = [self.statuses firstObject];
    if (status) {
        params[@"since_id"] = status.idstr;
    }
    [requestManager GET:urlString parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *  _Nullable responseObject) {
        NSArray *newStatuses = [DJStatus mj_objectArrayWithKeyValuesArray:responseObject[@"statuses"]];
        // 将刷新获取到的新数据添加到总数组的头部
        NSRange range = NSMakeRange(0, newStatuses.count);
        NSIndexSet *indexSet = [[NSIndexSet alloc] initWithIndexesInRange:range];
        [self.statuses insertObjects:newStatuses atIndexes:indexSet];
        // 刷新TableView
        [self.tableView reloadData];
        // 隐藏RefreshControl
        [control endRefreshing];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [control endRefreshing]; // 当数据获取失败时结束刷新操作
    }];
    

}

 

最终效果:

技术分享

 

新浪微博客户端(18)-集成下拉刷新控件UIRefreshControl

标签:nbsp   ogr   sar   failure   stat   val   selector   request   控件   

原文地址:http://www.cnblogs.com/yongdaimi/p/6024707.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!