- (void)viewDidLoad {
[super viewDidLoad];
//0.集成刷新控件
[self setupRefreshView];
//1.设置导航栏内容
[self setupNavBar];
//2.加载微薄数据
[self setupStatusData];
}
/**
* 集成刷新控件
*/
- (void)setupRefreshView
{
UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
[refreshControl addTarget:self action:@selector(refreshControlStateChnage:) forControlEvents:UIControlEventValueChanged];
[self.tableView addSubview:refreshControl];
}
/**
* 监听refreshControl控件值的改变事件
*/
- (void)refreshControlStateChnage:(UIRefreshControl *)refreshControl
{
//向新浪获取最新的微博数据
//1.创建请求管理对象
AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
//2.封装请求参数
NSMutableDictionary *params = [NSMutableDictionary dictionary];
params[@"access_token"] = [AccountTool account].access_token;
params[@"count"] = @10;
StatusFrame *statusFrame = self.statusFrames[0];
params[@"since_id"] = statusFrame.status.idstr; //返回ID比since_id大的微博
//3.发送
[mgr GET:@"https://api.weibo.com/2/statuses/home_timeline.json" parameters:params
success:^(AFHTTPRequestOperation *operation, id responseObject) {
//将字典数组转为模型数组(里边放的就是Status模型)
NSArray *statusArray = [Status objectArrayWithKeyValuesArray:responseObject[@"statuses"]];
//创建frame模型对象
NSMutableArray *statusFrameArray = [NSMutableArray array];
for (Status *status in statusArray) {
StatusFrame *statusF = [[StatusFrame alloc]init];
//传递微博模型数据
statusF.status = status;
[statusFrameArray addObject:statusF];
}
//将最新的数据追加到旧数据的前面
//旧数据:self.statusFrames
//新数据:statusFrameArray
NSMutableArray *tempArray = [NSMutableArray array];
[tempArray addObjectsFromArray:statusFrameArray];
[tempArray addObjectsFromArray:self.statusFrames];
self.statusFrames = tempArray;
//5.刷新表格
[self.tableView reloadData];
//6.让刷新控件停止转动
[refreshControl endRefreshing];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//6.让刷新控件停止转动
[refreshControl endRefreshing];
}];
}
系统UIRefreshControl 的简单使用代码
最新推荐文章于 2017-10-10 19:32:22 发布