//
// Hearo.h
// 0928英雄展示
//
// Created by weibiao on 15-9-28.
// Copyright (c) 2015年 weibiao. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Hearo : NSObject
//图片
@property (nonatomic, copy) NSString *icon;
//描述
@property (nonatomic, copy)NSString *intro;
//标题
@property (nonatomic, copy)NSString *name;
-(instancetype)initWithDict:(NSDictionary *)dict;
+(instancetype)hearoWithDict:(NSDictionary *)dict;
@end
//
// Hearo.m
// 0928英雄展示
//
// Created by weibiao on 15-9-28.
// Copyright (c) 2015年 weibiao. All rights reserved.
//
#import "Hearo.h"
@implementation Hearo
+(instancetype)hearoWithDict:(NSDictionary *)dict {
return [[self alloc] initWithDict:dict];
}
-(instancetype)initWithDict:(NSDictionary *)dict {
if (self =[super init]) {
[self setValuesForKeysWithDictionary:dict];
}
return self;
}
@end
//
// ViewController.m
// 0928英雄展示
//
// Created by weibiao on 15-9-28.
// Copyright (c) 2015年 weibiao. All rights reserved.
//
#import "ViewController.h"
#import "Hearo.h"
@interface ViewController ()<UITableViewDataSource, UITableViewDelegate>//2.设置协议
@property (weak, nonatomic) IBOutlet UITableView *tableView;
//用于保存所有英雄数据的数组
@property (nonatomic, strong) NSArray *hearos;
@end
@implementation ViewController
#pragma mark - 懒加载
-(NSArray *)hearos {
if (_hearos == nil) {
//1.获得全路径
NSString *fullPath = [[NSBundle mainBundle] pathForResource:@"heros.plist" ofType:nil];
//2.根据全路径加载数据
NSArray *dictArray = [NSArray arrayWithContentsOfFile:fullPath];
//3.字典转模型
NSMutableArray *models = [NSMutableArray arrayWithCapacity:dictArray.count];
for (NSDictionary *dict in dictArray) {
Hearo *hero = [Hearo hearoWithDict:dict];
[models addObject:hero];
}
//4.赋值数据
_hearos = [models copy];
}
//5.返回数据
return _hearos;
}
- (void)viewDidLoad {
[super viewDidLoad];
//1.首先设置数据源
self.tableView.dataSource = self;//控制器就是数据源
}
#pragma mark - UITableViewDataSource
//3.返回的组数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
//4.返回的每组的行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.hearos.count;
}
//5.返回的每一组的每一行的单元格内容
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//1.创建单元格
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
//取出模型
Hearo *hero = self.hearos[indexPath.row];
//2.设置单元格数据
//2.1设置主标题
cell.textLabel.text = hero.name;
//2.2设置子标题
cell.detailTextLabel.text = hero.intro;
//2.3设置图片
cell.imageView.image = [UIImage imageNamed:hero.icon];
//3.放回单元格
return cell;
}
//6.设置头部标题
//-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
// return @"英雄展示";
//}
//7.设置尾部标题
//-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
// return @"展示完毕";
//}
//隐藏状态栏
-(BOOL)prefersStatusBarHidden {
return YES;
}
@end