UI_model传值, json数据解析
2015-08-17 21:10
399 查看
model 传值
首先建一个作为model的类, 继承于NSObject
接口文件写属性 , 实现文件里写属性的释放和一个 容错方法 (KVC)
如果请求图片的网络地址, 需要在工程里添加一个SDWebImage的第三方, 并引头文件 #import”UIImageView + WebCache ”
-请求方法如下:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
豆瓣电影 , 电影界面
先写一个model 类 , 有2条属性 , 用来传值
在视图控制器里铺一个tableView, 进行基本设置 , 然后进行解析, 主要代码如下
使用本地movieList.txt文档 进行数据解析
请求网络数据, 进行数据解析
显示数据
首先建一个作为model的类, 继承于NSObject
接口文件写属性 , 实现文件里写属性的释放和一个 容错方法 (KVC)
- (void)setValue:(id)value forUndefinedKey:(NSString *)key{ }
如果请求图片的网络地址, 需要在工程里添加一个SDWebImage的第三方, 并引头文件 #import”UIImageView + WebCache ”
-请求方法如下:
[承接图片的视图 sd_setImageWithURL:[NSURL URLWithString:图片地址]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
豆瓣电影 , 电影界面
先写一个model 类 , 有2条属性 , 用来传值
// movie.h 文件 @property(nonatomic, copy)NSString *movieName; // 电影名 @property(nonatomic, copy)NSString *pic_url; // 图片 // movie.m 文件 - (void)setValue:(id)value forUndefinedKey:(NSString *)key{ // KVC 容错方法 } - (void)dealloc { [_movieId release]; [_movieName release]; [super dealloc]; }
在视图控制器里铺一个tableView, 进行基本设置 , 然后进行解析, 主要代码如下
使用本地movieList.txt文档 进行数据解析
NSString *path = [NSBundle mainBundle]pathForResource@"movie list" ofType@"txt"]; NSData *data = [NSData dataWithContentOffile:path] // json 解析 NSMutableDictionary *dic = [NSJSONSerialization options:NSJSONReadingMutableContainers error:nil]; for (NSMutableDictionary *temp in dic[@"result"]) { Movie *mov = [[Movie alloc]init]; [mov setValuesForKeysWithDictionary:temp]; [self.movieArr addObject:mov]; [mov release]; }
请求网络数据, 进行数据解析
NSString *strURL = @"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/movielist.php"; NSURL *url = [NSURL URLWithString:strURL]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; // 异步get请求 (block 方式) [NSURLConnection sendAsynchronousRequest:request completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { // 对data进行数据处理 //json 解析 NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil]; for (NSMutableDictionary *temp in dic[@"result"]) { Movie *mov = [[Movie alloc]init]; [mov setValuesForKeysWithDictionary:temp]; [self.movieArr addObject:mov]; [mov release]; } [self.movieTableView reloadData]; }];
显示数据
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return self.movieArr.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *reuse = @"reuse"; UITableViewCell *cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:reuse]; if (!cell) { cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse] autorelease]; } Movie *movie = self.movieArr[indexPath.row]; cell.textLabel.text = movie.movieName; [cell.imageView sd_setImageWithURL:[NSURL URLWithString:movie.pic_url]; return cell; }
相关文章推荐
- LeetCode之Unique Binary Search Trees II
- hdu 3836 Equivalent Sets(tarjan+缩点)
- UITableView之上拉刷新
- IOS UITableView 图片文字重叠问题
- POJ 题目3080 Blue Jeans(KMP+暴力)
- Powerbuilder 连接字符串各个参数 Database parameters and supported database interfaces
- UIView添加手势 然后UITableView 添加进这个View 导致UITableView 的单元格点击事件无效
- uva 1342 - That Nice Euler Circuit(欧拉定理)
- 1085. Perfect Sequence (25)
- Squence 设置主键自动增长,设置起始值、步长
- Quick Reference: git 的.gitignore文件
- [POJ2478]Farey Sequence
- UIMenuController 的使用指南
- HDU2227(Find the nondecreasing subsequences)
- 对UITableView的一些设置
- Light OJ 1188 Fast Queries(分块暴力)
- POJ3038(Blue Jeans)
- QuickSort 快速排序 基于伪代码实现
- easyui 快速开发整理
- iOS开发-Day25-UIView