//
// AppDelegate.h
// UI11_练习2
//
// Created by YIem on 15/12/21.
// Copyright © 2015年 www.yiem.net YIem博客. All rights reserved.
//

import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (retain, nonatomic) UIWindow *window;

@end


//
// AppDelegate.m
// UI11_练习2
//
// Created by YIem on 15/12/21.
// Copyright © 2015年 www.yiem.net YIem博客. All rights reserved.
//

import "AppDelegate.h"

import "ViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

  • (void)dealloc
    {
    [_window release];
    [super dealloc];
    }
  • (BOOL)application:(UIApplication )application didFinishLaunchingWithOptions:(NSDictionary )launchOptions {
    // Override point for customization after application launch.
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    ViewController *rootVC = [[ViewController alloc] init];
    UINavigationController *navi = [[UINavigationController alloc]initWithRootViewController:rootVC];
    self.window.rootViewController = navi;
    [rootVC release];
    [navi release];

    [_window release];
    return YES;
    }

  • (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }
  • (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }
  • (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }
  • (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }
  • (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }

@end



//
// ViewController.h
// UI11_练习2
//
// Created by YIem on 15/12/21.
// Copyright © 2015年 www.yiem.net YIem博客. All rights reserved.
//

import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@end


//
// ViewController.m
// UI11_练习2
//
// Created by YIem on 15/12/21.
// Copyright © 2015年 www.yiem.net YIem博客. All rights reserved.
//

import "ViewController.h"

import "MovieCell.h"

import "SecondViewController.h"

import "MovieModel.h"

@interface ViewController ()<UITableViewDataSource, UITableViewDelegate, PassDelegate>
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *dataArr;
@end

@implementation ViewController

  • (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"练习";
    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    [_tableView release];

    // 数据处理

    [self dataHandler];

    }

  • (void)dataHandler
    {
    NSString *path = [[NSBundle mainBundle]pathForResource:@"movie" ofType:@"plist"];
    // self.dataArr = [[NSArray arrayWithContentsOfFile:path] mutableCopy];
    // 目标: 数组+字典 -> 数组 + model对象
    // 保存数据源
    NSArray *arr = [NSArray arrayWithContentsOfFile:path];
    // 初始化可变数组
    self.dataArr = [NSMutableArray array];
    // 遍历 数据源
    for (NSDictionary *dic in arr) {

    // 每遍历一个字典 创建一个对象
    MovieModel *m = [[MovieModel alloc]init];
    // 给对象赋值(KVC)
    [m setValuesForKeysWithDictionary:dic];
    m.movieid = dic[@"id"];
    // 把对象添加到数组
    [_dataArr addObject:m];
    
    [m release];

    }

    }

  • (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
    return _dataArr.count;
    }
  • (UITableViewCell )tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    MovieCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    ;
    if (!cell) {

    cell = [[[MovieCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"] autorelease];

    }
    // 字典赋值
    // NSDictionary *dic = _dataArr[indexPath.row];

// cell.pubLabel.text = dic[@"pubdate"];
// cell.titleLabel.text = dic[@"title"];


// KVC赋值之后 cell的值
MovieModel *m = _dataArr[indexPath.row];
cell.titleLabel.text = m.title;
// cell.titleLabel.text = xx
cell.pubLabel.text = m.pubdate;


// 多态  ->子类指针 指向父类对象
// 子类对象的不同 可以呈现不同的结果

// NSObject *a = [[UITableViewCell alloc]init];
// NSObject *b = [[MovieCell alloc] init];




 return cell;

}

  • (CGFloat)tableView:(UITableView )tableView heightForRowAtIndexPath:(NSIndexPath )indexPath
    {
    return 140;
    }
  • (void)tableView:(UITableView )tableView didSelectRowAtIndexPath:(NSIndexPath )indexPath
    {
    // tableView滚动方法
    // [tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
    // 页面跳转
    SecondViewController *secVC = [[SecondViewController alloc]init];
    // 属性传值
    secVC.receiveValue = _dataArr[indexPath.row];
    // 代理人
    secVC.delegate = self;
    [self.navigationController pushViewController:secVC animated:YES];

    }

  • (void)passValue:(NSDictionary *)dic
    {
    [self.dataArr addObject:dic];
    [self.tableView reloadData];
    }
  • (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    }

@end



//
// MovieCell.h
// UI11_练习2
//
// Created by YIem on 15/12/21.
// Copyright © 2015年 www.yiem.net YIem博客. All rights reserved.
//

import <UIKit/UIKit.h>

@interface MovieCell : UITableViewCell
@property (nonatomic, retain) UILabel *titleLabel;
@property (nonatomic, retain) UILabel *pubLabel;
@end


//
// MovieCell.m
// UI11_练习2
//
// Created by YIem on 15/12/21.
// Copyright © 2015年 www.yiem.net YIem博客. All rights reserved.
//

import "MovieCell.h"

@implementation MovieCell

  • (void)dealloc
    {
    [_titleLabel release];
    [_pubLabel release];
    [super dealloc];
    }
  • (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
    {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {

    // title
    self.titleLabel = [[UILabel alloc]init];
    _titleLabel.backgroundColor = [UIColor redColor];
    [self.contentView addSubview:_titleLabel];
    [_titleLabel release];
    // pubdate
    self.pubLabel = [[UILabel alloc]init];
    _pubLabel.backgroundColor  = [UIColor yellowColor];
    [self.contentView addSubview:_pubLabel];
    [_pubLabel release];

    }
    return self;
    }

  • (void)layoutSubviews
    {
    [super layoutSubviews];
    _titleLabel.frame = CGRectMake(20, 20, self.contentView.frame.size.width - 40, 40);
    CGRect f = _titleLabel.frame;
    f.origin.y += 60;
    _pubLabel.frame = f;
    }
  • (void)awakeFromNib {
    // Initialization code
    }
  • (void)setSelected:(BOOL)selected animated:(BOOL)animated {

    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state

    }

@end



//
// SecondViewController.h
// UI11_练习2
//
// Created by YIem on 15/12/21.
// Copyright © 2015年 www.yiem.net YIem博客. All rights reserved.
//

import <UIKit/UIKit.h>

@protocol PassDelegate <NSObject>

  • (void)passValue:(NSDictionary *)dic;

@end
@interface SecondViewController : UIViewController
@property (nonatomic, retain) NSDictionary *receiveValue;
@property (nonatomic, assign) id<PassDelegate>delegate;
@end


//
// SecondViewController.m
// UI11_练习2
//
// Created by YIem on 15/12/21.
// Copyright © 2015年 www.yiem.net YIem博客. All rights reserved.
//

import "SecondViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController

  • (void)viewDidLoad {

    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor  = [UIColor whiteColor];
    
    
     NSLog(@"%@", self.receiveValue);
    
    // back
    UIButton *back  = [UIButton buttonWithType:UIButtonTypeSystem];
    back.frame = CGRectMake(100, 100, 100, 100);
    back.backgroundColor = [UIColor redColor];
    [back addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:back];
    

}

  • (void)back
    {

    NSDictionary *dic = @{@"title": @"这是一个电影名字", @"pubdate": @"2015-12-21"};
    [self.delegate passValue:dic];
    [self.navigationController popViewControllerAnimated:YES];

    }

  • (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.

    }

/*

pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation

  • (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.

    }

*/

@end



//
// MovieModel.h
// UI11_练习2
//
// Created by YIem on 15/12/21.
// Copyright © 2015年 www.yiem.net YIem博客. All rights reserved.
//

import <Foundation/Foundation.h>

@interface MovieModel : NSObject

warning 使用KVC赋值的model类中 定义的属性名 一定要和字典中的key值相同(一般情况下) 数据类型也要保证一致

@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *pubdate;
@property (nonatomic, retain) NSNumber *wish;
@property (nonatomic, copy) NSString *movieid;
@end


//
// MovieModel.m
// UI11_练习2
//
// Created by YIem on 15/12/21.
// Copyright © 2015年 www.yiem.net YIem博客. All rights reserved.
//

import "MovieModel.h"

@implementation MovieModel

  • (void)dealloc
    {

    [_title release];
    [_pubdate release];
    [super dealloc];

    }

// KVC容错方法(赋值)

  • (void)setValue:(id)value forUndefinedKey:(NSString *)key
    {

    // 当key找不到对应的属性时 表示不需要该数据

    }

// KVC容错方法(取值)
/*

  • (id)valueForUndefinedKey:(NSString *)key
    {
    return nil;
    }

*/

@end