iOS-新浪微博开发-26-下拉刷新
来源:互联网 发布:2017网络上很火的神曲 编辑:程序博客网 时间:2024/04/30 09:42
在这里使用的是苹果系统原生的下拉刷新控件
用起来很简单 上代码 和大家一起分享
但是苹果没有为我们提供上拉刷新的控件
#import "QHHomeViewController.h"#import "QHDropdownMenu.h"#import "QHTitleMenuTableViewController.h"#import "AFNetworking.h"#import "QHAccountTool.h"#import "QHTitleButton.h"#import "UIImageView+WebCache.h"#import "QHUser.h"#import "QHStatus.h"#import "MJExtension.h"//https://api.weibo.com/2/users/show.json@interface QHHomeViewController ()<QHDropdownMenuDelegate>/** * 微博数组(里面放的都是字典 每个字典代表一条微博) 改为:微博数组(里面放的都是QHStatus 模型) 一个QHStatus 模型对应一条微博 */@property(nonatomic,strong)NSMutableArray *statues;@end@implementation QHHomeViewController- (NSMutableArray *)statues{ if (!_statues) { self.statues = [NSMutableArray array]; } return _statues;}- (void)viewDidLoad { [super viewDidLoad]; //设置导航栏 [self setupNav]; //获取用户信息(昵称) [self setupUserInfo]; // //加载最新的微博数据// [self loadNewStatus]; //集成刷新控件 [self setupRefresh]; }/** * 集成刷新控件 * * @return */#warning 对于没有使用过的方法要学会猜- (void)setupRefresh{ UIRefreshControl *control = [[UIRefreshControl alloc]init]; [self.tableView addSubview:control]; [control addTarget:self action:@selector(refreshStateChange:) forControlEvents:UIControlEventValueChanged];}/** * UIRefreshControl 进入刷新状态:加载最新数据 *///since_id false int64 若指定此参数,则返回ID比since_id大的微博(即比since_id时间晚的微博),默认为0。- (void)refreshStateChange:(UIRefreshControl *)control{ QHLog(@"refreshStateChange"); //1.请求管理者 AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager]; //2.拼接参数 NSMutableDictionary *params = [NSMutableDictionary dictionary]; QHAccount *account = [QHAccountTool account]; params[@"access_token"] = account.access_token; //取出最前面的微博(最新的微博 ID最大的微博) QHStatus *firstStatus = [self.statues firstObject];#warning 如果这里手动刷新 需要判断第一条微博数据是否为空 if(firstStatus) { //若指定次参数 则返回ID 比since_id 大的微博(即比since_id 时间晚的微博)默认为0 params[@"since_id"] =firstStatus.idstr; } //3.发送请求 [mgr GET:@"https://api.weibo.com/2/statuses/friends_timeline.json" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) { // 将微博字典转化成 微博模型的数组 NSArray *newStatuses = [QHStatus objectArrayWithKeyValuesArray:responseObject[@"statuses"]]; //将最新的微博数据 添加到总数组的最前面 NSRange range = NSMakeRange(0, newStatuses.count); NSIndexSet *set = [NSIndexSet indexSetWithIndexesInRange:range]; [self.statues insertObjects:newStatuses atIndexes:set]; //刷新表格 [self.tableView reloadData]; [control endRefreshing]; } failure:^(AFHTTPRequestOperation *operation, NSError *error) { QHLog(@"请求失败 -- %@",error); [control endRefreshing]; }]; }/** * 加载最新的微博数据 */- (void)loadNewStatus{ //https://api.weibo.com/2/statuses/friends_timeline.json //1.请求管理者 AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager]; //2.拼接参数 NSMutableDictionary *params = [NSMutableDictionary dictionary]; QHAccount *account = [QHAccountTool account]; params[@"access_token"] = account.access_token;// params[@"count"] = @10; //我们想获取哪些信息直接传参数就可以了 //3.发送请求 [mgr GET:@"https://api.weibo.com/2/statuses/friends_timeline.json" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {// QHLog(@"请求成功%@",responseObject); //取得"微博 字典"数组// NSArray *dictArray = responseObject[@"statuses"]; //将 “微博字典”数组 转为 “微博模型”数组// for(NSDictionary *dict in dictArray)// {// QHStatus *status = [QHStatus objectWithKeyValues:dict];// [self.statues addObject:status];// } //将 “微博字典”数组 转为 “微博模型”数组 NSArray *newStatuses = [QHStatus objectArrayWithKeyValuesArray:responseObject[@"statuses"]]; //将最新的微博数据 添加到总数组后面 //self.statues = @[] //newStatuses = @[status01,status02] [self.statues addObjectsFromArray:newStatuses]; //self.statuses == @[status01,status02]; //下面做法是错误的!!!// [self.statues addObject:newStatuses]; //self.statues == @[@[status01,ststus02]]; //刷新表格 [self.tableView reloadData]; } failure:^(AFHTTPRequestOperation *operation, NSError *error) { QHLog(@"请求失败-%@",error); }];}/** * 获得用户信息(昵称) */- (void)setupUserInfo{ //https://api.weibo.com/2/users/show.json //access_token false string 采用OAuth授权方式为必填参数,其他授权方式不需要此参数,OAuth授权后获得。 //uid false int64 需要查询的用户ID。 //1.请求管理者 AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager]; //2.拼接请求参数 QHAccount *account = [QHAccountTool account]; NSMutableDictionary *params = [NSMutableDictionary dictionary]; params[@"access_token"] = account.access_token; params[@"uid"] =account.uid; //3.发送请求 [mgr GET:@"https://api.weibo.com/2/users/show.json" parameters:params success:^(AFHTTPRequestOperation *operation, NSDictionary * responseObject) { QHLog(@"请求成功%@",responseObject); //标题按钮 UIButton *titleButton = (UIButton *)self.navigationItem.titleView; //设置名字// NSString *name = responseObject[@"name"]; QHUser *user = [QHUser objectWithKeyValues:responseObject]; [titleButton setTitle:user.name forState:UIControlStateNormal];// [titleButton sizeToFit]; //存储昵称到沙盒中 account.name = user.name; [QHAccountTool saveAccount:account]; } failure:^(AFHTTPRequestOperation *operation, NSError *error) { QHLog(@"请求失败-- %@",error); }];}/** * 设置导航栏 */- (void)setupNav{ //这时self.view.window 值为空 NSLog(@"%@",self.view.window); /*设置导航栏上面的内容*/ //注意这一调用的是控制器的方法 Tool 里面没有方法 知识调用action 的方法 self.navigationItem.leftBarButtonItem =[UIBarButtonItem itemWithTarget:self Action:@selector(friendSearch) image:@"navigationbar_friendsearch" highImage:@"navigationbar_friendsearch_highlighted"]; self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithTarget:self Action:@selector(pop) image:@"navigationbar_pop" highImage:@"navigationbar_pop_highlighted"]; QHLog(@"QHHomeViewController"); /*中间的标题按钮*/ QHTitleButton *titleButton = [[QHTitleButton alloc]init];// titleButton.width = 150;// titleButton.height = 30; //titleButton.backgroundColor = QHRandomColor; //设置图片和文字 NSString * name = [QHAccountTool account].name; [titleButton setTitle:name?name:@"首页" forState:UIControlStateNormal]; // titleButton.imageEdgeInsets = UIEdgeInsetsMake(0, 90, 0, 0);// titleButton.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 40); self.navigationItem.titleView = titleButton; //按钮的自适应 内部的内容有多大 按钮就不用设置 代替了实质宽高// [titleButton sizeToFit]; //如果图片的某个方向上不规则 比如突起 那么这个方向就不能拉伸 //监听标题的点击 [titleButton addTarget:self action:@selector(titleClick:) forControlEvents:UIControlEventTouchUpInside]; /** * 解决方案 转换坐标系 * * */ }/** * 标题点击 */-(void)titleClick:(UIButton *)titleButton{ //1.创建下拉菜单 QHDropdownMenu *menu = [QHDropdownMenu menu]; menu.delegate = self; //2.设置内容 //menu.content = [UIButton buttonWithType:UIButtonTypeContactAdd]; //menu.content = [[UITableView alloc]initWithFrame:CGRectMake(0,0 , 100, 100) style:UITableViewStylePlain]; QHTitleMenuTableViewController *vc = [[QHTitleMenuTableViewController alloc]init]; vc.view.height = 44*3; vc.view.width = 150;#warning mark 在里面保存了全局变量 所以不会被销毁 menu.contentController = vc; //3.显示 [menu showFrom:titleButton]; //4.让箭头向上 // [menu dismiss]; }#pragma mark - 代理方法QHDropdownMenuDelegate/** * 下拉菜单被销毁了 向下 * * @param menu <#menu description#> */- (void)dropdownMenueDidDismiss:(QHDropdownMenu *)menu{ UIButton *titleButton = (UIButton *)self.navigationItem.titleView; titleButton.selected = NO; // [titleButton setImage:[UIImage imageNamed:@"navigationbar_arrow_down"] forState:UIControlStateNormal];}/** * 下拉菜单显示了 向上 * * @param menu <#menu description#> */- (void)dropdownMenueDidShow:(QHDropdownMenu *)menu{ UIButton *titleButton = (UIButton *)self.navigationItem.titleView; titleButton.selected = YES; //[titleButton setImage:[UIImage imageNamed:@"navigationbar_arrow_up"] forState:UIControlStateNormal];}-(void)friendSearch{ NSLog(@"friendsearch");}-(void)pop{ NSLog(@"pop");}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}#pragma mark - Table view data source- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.statues.count;}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *ID = @"status"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; if (cell == nil) { cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID]; } //取出这行对应微博字典// NSDictionary *status = self.statues[indexPath.row]; QHStatus *status = self.statues[indexPath.row]; //取出这条微博的作者(用户)// NSDictionary *user = status[@"user"];// cell.textLabel.text = user[@"name"]; QHUser *user = status.user; cell.textLabel.text = user.name; //设置微博文字// cell.detailTextLabel.text = status[@"text"]; cell.detailTextLabel.text = status.text; //设置头像// NSString *imageUrl = user[@"profile_image_url"]; NSString *imageUrl = user.profile_image_url; UIImage *placehoder = [UIImage imageNamed:@"avatar_default_small"]; [cell.imageView sd_setImageWithURL:[NSURL URLWithString:imageUrl] placeholderImage:placehoder]; //QHLog(@"%@",user); return cell;}/** * 我们所要处理的问题 1.将字典转为模型 2.能够下拉刷新最新的微博数据 3.能够上拉加载更多的微博数据 */@end
0 0
- iOS-新浪微博开发-26-下拉刷新
- Android新浪微博下拉刷新
- Android新浪微博下拉刷新
- iOS开发 ----- 下拉刷新
- 新浪微博下拉刷新功能的实现
- Android模仿新浪微博(自定义ListView下拉刷新)
- iOS开发---MJRefresh下拉刷新/下拉加载
- ios开发-新浪微博08-下拉菜单的基本实现
- ios开发-新浪微博-09(下拉菜单封装一)
- ios开发-新浪微博10-(下拉菜单的二次封装 完整版)
- 【iOS开发】---- 下拉刷新(附Demo)
- IOS开发——TabelView下拉刷新
- iOS 开发:上拉加载,下拉刷新
- iOS开发技巧 UIRefreshControl下拉刷新
- iOS开发笔记>> 下拉刷新,自定义UIControl
- 新浪微博开发七(下拉菜单的实现)
- IOS 分享新浪微博开发通知
- 新浪微博ios客户端 开发流程
- 加油吧
- 度量学习
- 动画——View Animation,Drawable Animation,Property Animation,LayoutTransition
- [bzoj3566][SHOI2014]概率充电器
- 如何学好游戏编程
- iOS-新浪微博开发-26-下拉刷新
- 类方法
- 指针的指针
- 【软考】软考视频总结
- 行业门户网站、论坛的微信内容运营策划
- 《剑指offer》面试题:顺时针打印矩阵
- Android 使用Mob移动开发SDK笔记
- c# font gdicharset
- ARQ与滑动窗口