UI_model传值, json数据解析

  • model 传值
    • 首先建一个作为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;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值