本节重点:
了解UItableView 的基本属性
了解KVC... 其实 我也不是很了解
效果图:
代码示例:
控制器代码:
//
// SJViewController.m
// 03.LOL Demo
//
// Created by SJ.abnormal on 15-2-7.
// Copyright (c) 2015年 SJ.abnormal. All rights reserved.
//
#import "SJViewController.h"
#import "SJHero.h" //导入模型类
@interface SJViewController ()<UITableViewDataSource> //实现协议
@property (strong, nonatomic) UITableView *tableView;
@property (nonatomic, strong) NSArray *heroData;
@end
@implementation SJViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//设置数据源代理
self.tableView.dataSource = self;
}
//必须实现的三个方法
#pragma mark - 设置UITableView显示几组
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
#pragma mark - 设置UITableView第几组显示几行
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.heroData.count; //返回模型数组长度
}
#pragma mark - 设置cell显示的内容
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *ID = @"hero";
//取出对顶索引的模型
SJHero *data = self.heroData[indexPath.row];
//查找缓冲池中的cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
}
//设置文本信息
cell.textLabel.text = data.name;
//设置详细信息
cell.detailTextLabel.text = data.intro;
//设置图片
cell.imageView.image = [UIImage imageNamed:data.icon];
//返回cell
return cell;
}
#pragma mark - tableView 懒加载
- (UITableView *)tableView {
if (!_tableView) {
_tableView = [[UITableView alloc]initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewStyleGrouped];
[self.view addSubview:_tableView];
}
return _tableView;
}
#pragma mark - tableView 懒加载
- (NSArray *)heroData {
if (!_heroData) {
//获取文件路径
NSString *path = [[NSBundle mainBundle] pathForResource:@"heros.plist" ofType:nil];
//将文件加载到数组中
NSArray *dataAray = [NSArray arrayWithContentsOfFile:path];
//创建模型数组 并设置长度
NSMutableArray *dictArray = [NSMutableArray arrayWithCapacity:dataAray.count];
//迭代取出数组中的数据,转换成模型
for (NSDictionary *dict in dataAray) {
[dictArray addObject:[SJHero heroWithDict:dict]];
}
//将转换好的模型数组赋值
_heroData = [dictArray copy];
}
return _heroData;
}
@end
模型声明:
//
// SJHero.h
// 03.LOL Demo
//
// Created by SJ.abnormal on 15-2-7.
// Copyright (c) 2015年 SJ.abnormal. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface SJHero : NSObject
//模型类
@property (nonatomic, copy) NSString *icon; //图标
@property (nonatomic, copy) NSString *name; //名称
@property (nonatomic, copy) NSString *intro; //详细介绍
//工厂方法
- (instancetype) initWithDict: (NSDictionary *) dict;
+ (instancetype) heroWithDict: (NSDictionary *) dict;
@end
模型实现类:
//
// SJHero.m
// 03.LOL Demo
//
// Created by SJ.abnormal on 15-2-7.
// Copyright (c) 2015年 SJ.abnormal. All rights reserved.
//
#import "SJHero.h"
@implementation SJHero
- (instancetype) initWithDict: (NSDictionary *) dict {
if (self = [super init]) {
//KVC存取
[self setValuesForKeysWithDictionary:dict];
}
return self;
}
+ (instancetype) heroWithDict: (NSDictionary *) dict {
return [[self alloc] initWithDict: dict];
}
@end