把请求数据映射成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
#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
相关文章推荐
- sql中的Tran和C#中的TransactionScope介绍
- nagios监控iptables状态
- 路由转发原则
- OpenStack Keystone配置产生错误Access denied for user 'keystone'@'10.0.2.15' (using password: YES)") None N
- TeamViewer远程唤醒主机实战教程(多图)
- 新工作要做的事宜
- oracle 备份数据库对象(存储过程PROCEDURE,FUNCTION,VIEW,TRIGGER...)
- Stack集合、queue集合、hashtable集合
- Android开发:shape和selector和layer-list的(详细说明)
- Drools入门-----------环境搭建,分析Helloworld Drools5.0的xls文件转drl文件提升解析效率 使用BRMS的Tomcat6.0配置
- cocos2d-x 3.6 程序流程
- freeswitch与E1设备对接方法
- UVA 11082 Matrix Decompressing 矩阵解压(最大流,经典)
- 对Java并发编程的几点思考
- ASP.NET DropDownList控件回传值无刷新操作。
- U-BOOT中mkconfig详解
- hdu 4442 Physical Examination
- 习题3.25 链表和数组对队列例程实现
- linux下 切换双显卡调整屏幕亮度(简单两条命令搞定)
- ruby里的坑