1.设置info允许网络请求
2.找到接口文件运行到鬼脸
3.Appdelegate指定默认根视图,并且建立MVC管理模式,建立model类,以及自定义cellXIB类,model中图示:
这里主要是要在鬼脸接口文件中找到对应的对象名并且定义.
4.在自定义cell中设置xib的位置界面设置,并且通过拖拽完成在.h中定义属性
并且在.h声明方法- (void)setValueForCellWithBook:(Book *)book;
在.m中赋值,给出值,实现声明的方法
#import "BookCell.h"
@implementation BookCell
-(void)setValueForCellWithBook:(Book *)book{
if (book) {
self.imgV.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:book.iconUrl]]];
self.label1.text = book.name;
self.label2.text = book.expireDatetime;
self.label3.text = [NSString stringWithFormat:@"分享:%@次",book.shares];
self.label4.text = [NSString stringWithFormat:@"收藏:%@次",book.favorites];
self.label5.text = [NSString stringWithFormat:@"下载:%@次",book.downloads];
self.label6.text = book.categoryName;
self.label7.text = book.lastPrice;
}
}
@end
5.完成viewcontroller中的界面搭建并且请求解析数据如下
//
// ViewController.m
// 练习JSON解析
#import "ViewController.h"
#import "Book.h"
#import "BookCell.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic , strong)UITableView *ojtable;
@property(nonatomic , strong)NSMutableArray *arr;
@end
@implementation ViewController
- (UITableView *)ojtable{
if (!_ojtable) {
_ojtable = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
_ojtable.delegate = self;
_ojtable.dataSource =self;
}
return _ojtable;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title= @"降价";
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"设置" style:UIBarButtonItemStyleDone target:self action:nil];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"分类" style:UIBarButtonItemStyleDone target:self action:nil];
[self.view addSubview:self.ojtable];
[self.ojtable registerNib:[UINib nibWithNibName:@"BookCell" bundle:nil] forCellReuseIdentifier:@"cell"];
NSURL *url = [NSURL URLWithString:@"http://iappfree.candou.com:8080/free/applications/sales?page=1&number=1"];
NSData *data = [NSData dataWithContentsOfURL:url];
self.arr = [[NSMutableArray alloc]init];
NSError *error = nil;
NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
NSArray * array = dic[@"applications"];
for (NSDictionary *dic in array) {
Book *book = [Book new];
[book setValuesForKeysWithDictionary:dic];
[self.arr addObject:book];
}
[self.ojtable reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.arr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
BookCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (self.arr.count > 0) {
[cell setValueForCellWithBook:self.arr[indexPath.row]];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 140;
}
@end