1 // 2 // Hero.h 3 // LOLHero 4 // 5 // Created by hellovoidworld on 14/12/1. 6 // Copyright (c) 2014年 hellovoidworld. All rights reserved. 7 // 8 9 #import <Foundation/Foundation.h> 10 11 @interface Hero : NSObject 12 13 @PRoperty(nonatomic, copy) NSString *icon; 14 @property(nonatomic, copy) NSString *intro; 15 @property(nonatomic, copy) NSString *name; 16 17 - (instancetype) initWithDictionary:(NSDictionary *) dictionary; 18 + (instancetype) heroWithDictionary:(NSDictionary *) dictionary; 19 + (instancetype) hero; 20 21 @end
1 // 2 // Hero.m 3 // LOLHero 4 // 5 // Created by hellovoidworld on 14/12/1. 6 // Copyright (c) 2014年 hellovoidworld. All rights reserved. 7 // 8 9 #import "Hero.h" 10 11 @implementation Hero 12 13 - (instancetype) initWithDictionary:(NSDictionary *) dictionary { 14 if (self = [super init]) { 15 self.icon = dictionary[@"icon"]; 16 self.intro = dictionary[@"intro"]; 17 self.name = dictionary[@"name"]; 18 } 19 20 return self; 21 } 22 23 + (instancetype) heroWithDictionary:(NSDictionary *) dictionary { 24 return [[self alloc] initWithDictionary:dictionary]; 25 } 26 27 + (instancetype) hero { 28 return [self heroWithDictionary:nil]; 29 } 30 31 @end
1 // 2 // ViewController.m 3 // LOLHero 4 // 5 // Created by hellovoidworld on 14/12/1. 6 // Copyright (c) 2014年 hellovoidworld. All rights reserved. 7 // 8 9 #import "ViewController.h" 10 #import "Hero.h" 11 12 @interface ViewController () <UITableViewDataSource> 13 14 // UITableView 15 @property (weak, nonatomic) IBOutlet UITableView *tableView; 16 17 // 所有hero资料 18 @property(nonatomic, strong) NSArray *heros; 19 20 @end 21 22 @implementation ViewController 23 24 - (void)viewDidLoad { 25 [super viewDidLoad]; 26 // Do any additional setup after loading the view, typically from a nib. 27 28 // 设置dataSource 29 self.tableView.dataSource = self; 30 31 // 设置行高 32 self.tableView.rowHeight = 60; 33 } 34 35 - (void)didReceiveMemoryWarning { 36 [super didReceiveMemoryWarning]; 37 // Dispose of any resources that can be recreated. 38 } 39 40 /** 隐藏状态栏 */ 41 - (BOOL)prefersstatusBarHidden { 42 return YES; 43 } 44 45 /** 延迟加载hero数据 */ 46 - (NSArray *) heros { 47 if (nil == _heros) { 48 NSArray *dictArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"heros.plist" ofType:nil]]; 49 50 NSMutableArray *herosArray = [NSMutableArray array]; 51 for (NSDictionary *dict in dictArray) { 52 Hero *hero = [Hero heroWithDictionary:dict]; 53 [herosArray addObject:hero]; 54 } 55 56 _heros = herosArray; 57 } 58 59 return _heros; 60 } 61 62 #pragma mark - 列表方法 63 64 // section数, 默认是1 65 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 66 return 1; 67 } 68 69 // 特定section的行数 70 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 71 return self.heros.count; 72 } 73 74 75 // 特定行的内容 76 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 77 Hero *hero = self.heros[indexPath.row]; 78 79 // 必须使用"UITableViewCellStyleSubtitle"才能显示副标题 80 UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil]; 81 82 // 标题 83 cell.textLabel.text = hero.name; 84 85 // 副标题 86 cell.detailTextLabel.text = hero.intro; 87 88 // 图标 89 cell.imageView.image = [UIImage imageNamed:hero.icon]; 90 91 return cell; 92 } 93 94 @end