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

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

时间:2015-08-07 07:04:16      阅读:161      评论:0      收藏: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 

 

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

标签:

原文地址:http://www.cnblogs.com/lixiang2015/p/4709647.html

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