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

把请求数据映射成model类

时间:2015-07-14 20:02:55      阅读:116      评论:0      收藏:0      [点我收藏+]

标签:

添加一些第三方类库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

 

把请求数据映射成model类

标签:

原文地址:http://www.cnblogs.com/wtlovezjj/p/4646148.html

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