把请求数据映射成model类
添加一些第三方类库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