36.小项目:英雄列表 版本1.0

------------- ViewController.m -------------

#import "ViewController.h"

#import "CZHero.h"


@interface ViewController () <UITableViewDataSource, UITableViewDelegate>


@property (nonatomic,strong) NSArray *heros;

@property (weak, nonatomic) IBOutlet UITableView *tableView;


@end


@implementation ViewController


- (void)viewDidLoad

{

    [super viewDidLoad];

    self.tableView.rowHeight = 50;

}


- (BOOL)prefersStatusBarHidden

{

    return YES;

}


- (NSArray *)heros

{

    if (_heros == nil)

    {

        NSString *path = [[NSBundle mainBundle] pathForResource:@"heros.plist" ofType:nil];

        NSArray *dictArray = [NSArray arrayWithContentsOfFile:path];

        NSMutableArray *tmpArray = [NSMutableArray array];

        for (NSDictionary *dict in dictArray)

        {

            CZHero *hero = [CZHero heroWithDict:dict];

            [tmpArray addObject:hero];

        }

        _heros = tmpArray;

    }

    return _heros;

}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    return self.heros.count;

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];

    CZHero *hero = self.heros[indexPath.row];

    cell.textLabel.text = hero.name;

    cell.detailTextLabel.text = hero.intro;

    cell.imageView.image = [UIImage imageNamed:hero.icon];

    return cell;

}


@end 

------------- CGHero.h -------------

#import <Foundation/Foundation.h>


@interface CZHero : NSObject


@property (nonatomic,copy) NSString *icon;

@property (nonatomic,copy) NSString *intro;

@property (nonatomic,copy) NSString *name;

- (instancetype)initWithDict:(NSDictionary *)dict;

+ (instancetype)heroWithDict:(NSDictionary *)dict;


@end 

------------- CGHero.m -------------

#import "CZHero.h"


@implementation CZHero


- (instancetype)initWithDict:(NSDictionary *)dict

{

    if (self = [super init])

    {

        [self setValuesForKeysWithDictionary:dict];

    }

    return self;

}


+ (instancetype)heroWithDict:(NSDictionary *)dict

{

    return [[self alloc] initWithDict:dict];

}


@end 

 

posted @ 2015-08-07 02:08  我要选李白  阅读(154)  评论(0编辑  收藏  举报