您的位置:首页 > 其它

把请求数据映射成model类

2015-07-14 17:58 260 查看
添加一些第三方类库AFNetworking、JSONKit-NoWarning、OCMapper、SDWebImage

#import "AFNetworking.h"

#import "JSONKit.h"

#import "FirstModel.h"

#import "NSObject+ObjectMapper.h"

#import "SDImageCache.h"

#import "UIImageView+WebCache.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>

{

NSArray *_hotArray;

}

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

UITableView *tableview =[[UITableView alloc]initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];

tableview.delegate =self;

tableview.dataSource =self;

[self.view addSubview:tableview];

tableview.tableFooterView=[UIView new];

NSString *UrlStr =@"http://www.022eh.com/api/return_sub";

NSDictionary *dic =@{@"PageSize":@"3",@"index":@"1"};

AFHTTPRequestOperationManager *manager =[AFHTTPRequestOperationManager manager];

[manager POST:UrlStr parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject) {

NSLog(@"0000000=%@",operation.responseString);

NSDictionary *resat =[operation.responseData objectFromJSONData];

NSDictionary *data =[resat objectForKey:@"list"];

_hotArray =[FirstModel objectFromDictionary:data];

[tableview reloadData];

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {

}];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

return _hotArray.count;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:@"cell"];

if (!cell) {

cell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];

}

FirstModel *first =[_hotArray objectAtIndex:indexPath.row];

cell.textLabel.text=first.name;

[cell.imageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.022eh.com%@",first.img]] placeholderImage:[UIImage imageNamed:@"908fa0ec08fa513df6da05f9386d55fbb2fbd9a1.jpg"]];

return cell;

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

}

@end

firstmodel.h中添加

#import <Foundation/Foundation.h>

@interface FirstModel : NSObject

@property(nonatomic,strong)NSString *name;//要和所给数一致字母

@property(nonatomic)NSString *id;

@property(nonatomic,strong)NSString *img;

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: