码迷,mamicode.com
首页 > 其他好文 > 详细

电影项目 (二)

时间:2015-10-11 21:16:17      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:

显示界面如下 :(代码中有详细的注释)

 

技术分享

 

代码如下 :

HomeViewController控制器代码:

 

//
//  HomeViewController.m
//  Movie 2.0
//
//  Created by mac1 on 15/10/8.
//  Copyright (c) 2015年 www.iphonetrain.com. All rights reserved.
//

#import "HomeViewController.h"
#import "MovieModel.h"
#import "MovieCell.h"
#import "Common.h"

static NSString *iden = @"Movie_Cell";


@interface HomeViewController ()
//海报视图
@property (nonatomic,strong)UIView *posterView;

//电影列表视图
@property (nonatomic,strong)UITableView *listView;

//存储Model
@property (nonatomic, strong) NSMutableArray *movieData;

@end

@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //加载数据
    [self _loadData];
    
    
    // iOS7 之后,状态栏默认穿透效果 translucent
    // 穿透 = YES,视图的起始点坐标 屏幕的左上角
    // 穿透 = NO,视图的起始点坐标 导航栏下方左上角的点
    // self.navigationController.navigationBar.translucent = NO;
    

    //设置导航栏上右侧按钮
    [self _createNavigationRightItem];
    
    //创建海报视图
    [self _createPosterView];
    
    //创建电影列表视图
    [self _createListView];
    
    
    //测试Appearance
    /*
     UIButton *button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
     button.frame = CGRectMake(40, 100, 100, 100);
     
     [button addTarget:self
     action:@selector(btnAction)
     forControlEvents:UIControlEventTouchUpInside];
     
     [self.view addSubview:button];
     */
    
    
    UINib *nib = [UINib nibWithNibName:@"MovieCell" bundle:[NSBundle mainBundle]];
    
    //注册xib创建的单元格
    [_listView registerNib:nib forCellReuseIdentifier:iden];
    
    
    //注意坐标问题
    self.navigationController.navigationBar.translucent = NO;

}




//加载数据
- (void)_loadData{

    //1.加载json文件
    NSString *path = [[NSBundle mainBundle]pathForResource:@"new_movie" ofType:@"json"];

    //2.将json文件转化为二进制数据 NSData
    NSData *data = [NSData dataWithContentsOfFile:path];
    
    //系统提供的JSON序列化类
    /*
     data : 要转换的json数据
     options : 转换的选项.
     1.NSJSONReadingMutableContainers 生成可变的对象
     2.NSJSONReadingMutableLeaves 生成NSMutableString
     3.NSJSONReadingAllowFragments 生成不可变的对象
     */

    /*
     从对象 转换为 data
     options:
     NSJSONWritingPrettyPrinted 写入的对象格式很好看
     */
    
    //3.解析二进制数据 (系统提供的JSON序列化类)
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    //NSLog(@"dic = %@",dic);
    
    
    //4.把字典转换为model
    
    _movieData = [NSMutableArray array];
    
    //把字典转换为model
    //取到subjects数组
    NSArray *subjects = [dic objectForKey:@"subjects"];
    // subjects数组中,每一个小字典,都是一条数据(model)
    for (int i = 0; i < subjects.count; i++) {
        //一条数据
        NSDictionary *movieDic = subjects[i];
        
        //构造model对象
        MovieModel *model = [[MovieModel alloc] init];
        
        // model对象的属性赋值
        model.rating = [movieDic objectForKey:@"rating"];
        model.genres = [movieDic objectForKey:@"genres"];
        model.collect_count =
        [[movieDic objectForKey:@"collect_count"] integerValue];
        model.title = [movieDic objectForKey:@"title"];
        model.year = [movieDic objectForKey:@"year"];
        model.images = [movieDic objectForKey:@"images"];
        
        [self.movieData addObject:model];    }
   
}

//创建导航栏上右侧按钮
- (void)_createNavigationRightItem{

    //1.创建父视图
    UIView *rightView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 50, 30)];
    rightView.tag = 1000;
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithCustomView:rightView];
    self.navigationItem.rightBarButtonItem = rightItem;
    
    
    // 2.创建两个子视图
    //列表
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    button1.frame = CGRectMake(1, 2, 48, 26);
    [button1 setImage:[UIImage imageNamed:@"list_home"]
             forState:UIControlStateNormal];
    [button1 setBackgroundImage:[UIImage imageNamed:@"exchange_bg_home"]
                       forState:UIControlStateNormal];
    [button1 addTarget:self
                action:@selector(rightButtonAction:)
      forControlEvents:UIControlEventTouchUpInside];
    button1.tag = 1001;
    button1.hidden = YES;
    [rightView addSubview:button1];
    
    //海报
    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
    button2.frame = CGRectMake(1, 2, 48, 26);
    [button2 setImage:[UIImage imageNamed:@"poster_home"]
             forState:UIControlStateNormal];
    [button2 setBackgroundImage:[UIImage imageNamed:@"exchange_bg_home"]
                       forState:UIControlStateNormal];
    [button2 addTarget:self
                action:@selector(rightButtonAction:)
      forControlEvents:UIControlEventTouchUpInside];
    button2.tag = 1002;
    button2.hidden = NO;
    [rightView addSubview:button2];

    
}

//创建海报视图
- (void)_createPosterView{

    _posterView = [[UIView alloc] initWithFrame:self.view.bounds];
    _posterView.backgroundColor = [UIColor orangeColor];
    _posterView.hidden = NO;
    
    [self.view addSubview:_posterView];


}


//创建列表视图
- (void)_createListView {
    
    //NSLog(@"bounds = %@", NSStringFromCGRect(self.view.bounds));
    
    CGRect frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight - kTabBarHeight - kNavigationBarHeight);
    
    _listView = [[UITableView alloc] initWithFrame:frame
                                             style:UITableViewStylePlain];
    _listView.backgroundColor = [UIColor clearColor];
    _listView.hidden = YES;
    
    _listView.dataSource = self;
    //  _listView.rowHeight = 100;
    _listView.delegate = self;
    
    [self.view addSubview:_listView];
}

#pragma mark - buttonAction

- (void)rightButtonAction:(UIButton *)sender {

    //获取到需要翻转的两个button
    UIButton *button1 = (UIButton *)[self.navigationController.navigationBar viewWithTag:1001];
    UIButton *button2 = (UIButton *)[self.navigationController.navigationBar viewWithTag:1002];
    
    UIView *rightView =
    [self.navigationController.navigationBar viewWithTag:1000];
    
    //设置翻转的选项
    BOOL flag = button1.hidden;
    
    //三目运算符
    UIViewAnimationOptions flipOption =
    flag ? UIViewAnimationOptionTransitionFlipFromLeft
    : UIViewAnimationOptionTransitionFlipFromLeft;
    
    /*
     UIViewAnimationOptions flipOption;
     if (flag) {
     flipOption = UIViewAnimationOptionTransitionFlipFromLeft;
     } else {
     flipOption = UIViewAnimationOptionTransitionFlipFromLeft;
     }
     */
    
    //翻转动画
    button1.hidden = !button1.hidden;
    button2.hidden = !button2.hidden;
    
    //切换海报和列表视图
    _posterView.hidden = !_posterView.hidden;
    _listView.hidden = !_listView.hidden;
    
    //设置动画1
    [UIView transitionWithView:rightView duration:.35 options:flipOption animations:^{
        
        
    } completion:^(BOOL finished) {
        //动画完成执行的动画
        
    }];
    
    //设置动画2
    [UIView transitionWithView:self.view
                      duration:.35
                       options:flipOption
                    animations:^{
                        ;
                    }
                    completion:^(BOOL finished) {
                        ;
                    }];


}

#pragma mark - 表视图的数据源方法

//列数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

//行数
- (NSInteger)tableView:(UITableView *)tableView
 numberOfRowsInSection:(NSInteger)section {
    return self.movieData.count;
    
}


//创建单元格

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    MovieCell *cell = [tableView dequeueReusableCellWithIdentifier:iden];
    
    //注册就不需要这些
//    
//    if (cell == nil) {
//        
//        //加载xib单元格   (注意取出来是数组)
//        cell = [[[NSBundle mainBundle]loadNibNamed:@"MovieCell" owner:self options:nil] lastObject];
//        
//    }
    
    //将解析之后的数据交给单元格显示
    MovieModel *model = self.movieData
    [indexPath.row];
    
    cell.model = model;
    
    return cell;

    
}




//设置单元格的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 100;
    
}
@end

 

 

MovieModel模型代码 :

//
//  MovieModel.h
//  Movie 2.0
//
//  Created by mac1 on 15/10/8.
//  Copyright (c) 2015年 www.iphonetrain.com. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface MovieModel : NSObject


@property (nonatomic, strong) NSDictionary *rating; //评分
@property (nonatomic, strong) NSArray *genres; //类型
@property (nonatomic, assign) NSUInteger collect_count; //收藏数
@property (nonatomic, copy) NSString *title; //标题
@property (nonatomic, copy) NSString *original_title;
@property (nonatomic, copy) NSString *year; //年份
@property (nonatomic, strong) NSDictionary *images; //图片


@end


/*
 {
 "rating": {
 "max": 10,
 "average": 7.4,
 "stars": "40",
 "min": 0
 },
 "genres": [
 "喜剧",
 "奇幻",
 "古装"
 ],
 "collect_count": 61872,
 "casts": [
 {
 "avatars": {
 "small": "http://img4.douban.com/img/celebrity/small/21559.jpg",
 "large": "http://img4.douban.com/img/celebrity/large/21559.jpg",
 "medium": "http://img4.douban.com/img/celebrity/medium/21559.jpg"
 },
 "alt": "http://movie.douban.com/celebrity/1275542/",
 "id": "1275542",
 "name": "白百何"
 },
 ],
 "title": "捉妖记",
 "original_title": "捉妖记",
 "subtype": "movie",
 "directors": [
 {
 "avatars": {
 "small": "http://img4.douban.com/img/celebrity/small/42488.jpg",
 "large": "http://img4.douban.com/img/celebrity/large/42488.jpg",
 "medium": "http://img4.douban.com/img/celebrity/medium/42488.jpg"
 },
 "alt": "http://movie.douban.com/celebrity/1287124/",
 "id": "1287124",
 "name": "许诚毅"
 }
 ],
 "year": "2015",
 "images": {
 "small": "http://img4.douban.com/view/movie_poster_cover/ipst/public/p2257944916.jpg",
 "large": "http://img4.douban.com/view/movie_poster_cover/lpst/public/p2257944916.jpg",
 "medium": "http://img4.douban.com/view/movie_poster_cover/spst/public/p2257944916.jpg"
 },
 "alt": "http://movie.douban.com/subject/25723907/",
 "id": "25723907"
 }
 */

 

单元格是用xib画出来的.

MovieCell代码如下 :

//
//  MovieCell.m
//  Movie 2.0
//
//  Created by mac1 on 15/10/8.
//  Copyright (c) 2015年 www.iphonetrain.com. All rights reserved.
//

#import "MovieCell.h"
#import "MovieModel.h"
#import "UIImageView+WebCache.h"

@implementation MovieCell


//视图是xib文件创建会调用此方法
- (void)awakeFromNib{
    
    //取消单元格的选中效果
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    //清除单元格背景颜色
    self.backgroundColor = [UIColor clearColor];
    
}

- (void)setModel:(MovieModel *)model{
    
    _model = model;
    
    //1.单元格显示内容
    _titlelabel.text = model.title;
    
    //2.年份
    _yearLabel.text = [NSString stringWithFormat:@"上映年份 : %@", model.year];
    
    //图片地址字符串
    NSString *imageName = [model.images objectForKey:@"medium"];
    
    //URL
    NSURL *imageUrl = [NSURL URLWithString:imageName];
    ;
    
    //3.利用SDWebImaage设置网络图片
    [_postImageView sd_setImageWithURL:imageUrl];
    
    
}
@end

 

 

在编写代码过程中,有个错误纠结了好长时间,程序一直崩溃.运行提示错误是xib加载key没有value,找了好久的错误,原来原因是:自己给控件连线的时候选的是File‘s owner.....正确的应该是选择当前的单元格.

后面的页面框架界面每天都会有更新哟....这是监督自己复习的很好的方法,也是为找工作的很好的准备!!!

电影项目 (二)

标签:

原文地址:http://www.cnblogs.com/pengsi/p/4869837.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!