Cowboy Tech

iOS项目3.英雄图片表视图展示

效果图

效果图

项目结构

直接在Main Storyboard中拖入一个TableView

项目结构

字典加载数据模型

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

if (self=[super init]) {
    [self setValuesForKeysWithDictionary:dict];
}
return self;
}

加载plist中的数组数据

采用MVC模式,和前一项目相比,其数据加载时创建了一个Model

- (NSArray *)heros{

if (!_heros) {
    NSString *path = [[NSBundle mainBundle]pathForResource:@"heros" ofType:@"plist"];
    NSArray *array = [NSArray arrayWithContentsOfFile:path];
    NSMutableArray *arrayM = [NSMutableArray arrayWithCapacity:array.count];

    for (NSDictionary *dict in array) {

        JKHero *hero =  [JKHero heroWithDict:dict];

        [arrayM addObject:hero];
    }

    _heros = [arrayM copy];
}
return _heros;
}

表视图cell的配置

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

static NSString *identifier = @"heroCell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

if (!cell) {
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
}

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

cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.text = hero.name;
cell.detailTextLabel.text = hero.intro;
cell.detailTextLabel.textColor = [UIColor orangeColor];
cell.imageView.image = [UIImage imageNamed:hero.icon];

return cell;
}

隐藏状态栏

- (BOOL)prefersStatusBarHidden{
return YES;
}