1.面向字典开发-加载数据的过程
①向服务器发送请求
自定义:
@property (nonatomic,strong) NSArray *statuses;
-(void)setUpStatusData
{
// 1.创建请求管理对象
AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
// 2.封装请求参数
NSMutableDictionary *params = [NSMutableDictionary dictionary];
params[@"access_token"] = [MRAccountTool account].access_token;
// 3.发送请求
[mgr GET:@"https://api.weibo.com/2/statuses/home_timeline.json" parameters:params
success:^(AFHTTPRequestOperation *operation, id responseObject) {
// 取出所有的微博数据
self.statuses = responseObject[@"statuses"];
// 刷新表格
[self.tableView reloadData];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
}];
}
②得到返回参数,设置cell
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Potentially incomplete method implementation.
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return self.statuses.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 1.创建cell
static NSString *ID = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
}
// 2.设置cell的数据
// 微博的文字(内容)
NSDictionary *status = self.statuses[indexPath.row];
cell.textLabel.text = status[@"text"];
// 微博作者的昵称
NSDictionary *user = status[@"user"];
cell.detailTextLabel.text = user[@"name"];
// 微博作者的头像
NSString *iconUrl = user[@"profile_image_url"];
[cell.imageView setImageWithURL:[NSURL URLWithString:iconUrl] placeholderImage:[UIImage imageWithName:@"tabbar_compose_button"]];
return cell;
}
2.面向Model开发-加载数据的过程
MRStatus.h
#import <Foundation/Foundation.h>
@class MRUser;
@interface MRStatus : NSObject
/**
* 微博的内容(文字)
*/
@property (nonatomic, copy) NSString *text;
/**
* 微博的来源
*/
@property (nonatomic, copy) NSString *source;
/**
* 微博的ID
*/
@property (nonatomic, copy) NSString *idstr;
/**
* 微博的转发数
*/
@property (nonatomic, assign) int reposts_count;
/**
* 微博的评论数
*/
@property (nonatomic, assign) int comments_count;
/**
* 微博的作者
*/
@property (nonatomic, strong) MRUser *user;
+ (instancetype)statusWithDict:(NSDictionary *)dict;
- (instancetype)initWithDict:(NSDictionary *)dict;
@end
MRStatus.m
#import "MRStatus.h"
#import "MRUser.h"
@implementation MRStatus
+ (instancetype)statusWithDict:(NSDictionary *)dict
{
return [[self alloc] initWithDict:dict];
}
- (instancetype)initWithDict:(NSDictionary *)dict
{
if (self = [super init]) {
self.idstr = dict[@"idstr"];
self.text = dict[@"text"];
self.source = dict[@"source"];
self.reposts_count = [dict[@"reposts_count"] intValue];
self.comments_count = [dict[@"comments_count"] intValue];
self.user = [MRUser userWithDict:dict[@"user"]];
}
return self;
}
@end
#import <Foundation/Foundation.h>
@interface MRUser : NSObject
/**
* 用户的ID
*/
@property (nonatomic, copy) NSString *idstr;
/**
* 用户的昵称
*/
@property (nonatomic, copy) NSString *name;
/**
* 用户的头像
*/
@property (nonatomic, copy) NSString *profile_image_url;
+ (instancetype)userWithDict:(NSDictionary *)dict;
- (instancetype)initWithDict:(NSDictionary *)dict;
@end
MRUser.m
#import "MRUser.h"
@implementation MRUser
+ (instancetype)userWithDict:(NSDictionary *)dict
{
return [[self alloc] initWithDict:dict];
}
- (instancetype)initWithDict:(NSDictionary *)dict
{
if (self = [super init]) {
self.idstr = dict[@"idstr"];
self.name = dict[@"name"];
self.profile_image_url = dict[@"profile_image_url"];
// KVC : 会将字典的 所有 key-value(键值对) 赋值给模型对应的属性
// [self setValuesForKeysWithDictionary:dict];
}
return self;
}
@end
再控制器中的设置:
-(void)setUpStatusData
{
// 1.创建请求管理对象
AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
// 2.封装请求参数
NSMutableDictionary *params = [NSMutableDictionary dictionary];
params[@"access_token"] = [MRAccountTool account].access_token;
// 3.发送请求
[mgr GET:@"https://api.weibo.com/2/statuses/home_timeline.json" parameters:params
success:^(AFHTTPRequestOperation *operation, id responseObject) {
// 取出所有的微博数据(每一条微博都是一个字典)
NSArray *dictArray = responseObject[@"statuses"];
// 将字典数据转为模型数据
NSMutableArray *statusArray = [NSMutableArray array];
for (NSDictionary *dict in dictArray) {
// 创建模型
MRStatus *status = [MRStatus statusWithDict:dict];
// 添加模型
[statusArray addObject:status];
}
self.statuses = statusArray;
// 刷新表格
[self.tableView reloadData];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
}];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return self.statuses.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 1.创建cell
static NSString *ID = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
}
// 2.设置cell的数据
// 微博的文字(内容)
MRStatus *status = self.statuses[indexPath.row];
cell.textLabel.text = status.text;
// 微博作者的昵称
MRUser *user = status.user;
cell.detailTextLabel.text = user.name;
// 微博作者的头像
[cell.imageView setImageWithURL:[NSURL URLWithString:user.profile_image_url] placeholderImage:[UIImage imageWithName:@"tabbar_compose_button"]];
return cell;
}
3.使用MJExtension来简化
KVC的局限性:
使用KVC来设置,需要字典中的key和模型中的属性要一一对应,模型里面的属性个数要和字典中的key个数相同,并且对应的名称要一模一样。
由于MRStatus中有个User是个Model,通过setValue对应不上,顾KVC在这里失效
头文件中添加:#import "MJExtension.h"
在MRStatus和MRUser中去掉所有的方法,只保留属性。MJExtension主要用到了运行时识别的原理来进行属性的读取
-(void)setUpStatusData
{
// 1.创建请求管理对象
AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
// 2.封装请求参数
NSMutableDictionary *params = [NSMutableDictionary dictionary];
params[@"access_token"] = [MRAccountTool account].access_token;
// 3.发送请求
[mgr GET:@"https://api.weibo.com/2/statuses/home_timeline.json" parameters:params
success:^(AFHTTPRequestOperation *operation, id responseObject) {
// 取出所有的微博数据
self.statuses = [MRStatus objectArrayWithKeyValuesArray:responseObject[@"statuses"]];
// 刷新表格
[self.tableView reloadData];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
}];
}