这个是我做的第二个IOS小实例,比上一个微信消息简单了不少。但是使用的空间和原理有相似之处。
通过这个小实例的学习, 加深了对UITableView, UITableViewCell 的理解。
同时还了解了如何绑定plist文件中的数据。
先上个成果效果图:
创建步骤:
1. 拖动一个UITableView进入storyboard
2. 调整它的尺寸为4寸。拖入Hero.plist文件,并把需要用到的图片文件夹拖入image.xcassets.
3. 选中左侧tableview,单击右键
4. 在弹出的菜单中拖动关联datasource和delegate 去 ViewController.h。并且在ViewController.m中添加这些协议。
@interface ViewController () <UITableViewDataSource, UITableViewDelegate>
5. 创建Hero类, 创建Hero数据模型
6. 在Hero.h头文件中,把列表中用到的数据定义成其属性。
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *intro;
@property (nonatomic, copy) NSString *icon;
-(instancetype)initWithDict:(NSDictionary *)dict;
+(instancetype)heroWithDict:(NSDictionary *)dict;
7. 在Hero.m实施文件中,填写模型的实现方法
-(instancetype)initWithDict:(NSDictionary *)dict{
if(self = [super init]){
//way1:
// self.icon = [dict[@"icon"] copy];
// self.name = [dict[@"name"] copy];
// self.intro = [dict[@"intro"] copy];
//way2:
[self setValuesForKeysWithDictionary:dict];
}
return self;
}
//模型实例创建的工厂方法
+ (instancetype)heroWithDict:(NSDictionary *)dict{
return [[self alloc] initWithDict:dict];
}
8. 在ViewController.m中添加plist文件的读取方法
//lazy load
- (NSArray *)heros
{
if(!_heros){
NSString *path = [[NSBundle mainBundle] pathForResource:@"heros" ofType:@"plist"];//使用NSBundle获取文件路径
NSArray *arr = [NSArray arrayWithContentsOfFile:path];//使用文件创建数组
NSMutableArray *arrm = [NSMutableArray arrayWithCapacity:arr.count];//创建一个可变数组用来接收Hero对象
for (NSDictionary *dict in arr) {
Hero *hero = [Hero heroWithDict:dict];
[arrm addObject: hero];
}
_heros = [arrm copy];//可变数组的内容拷贝到数组
}
return _heros;
}
9. 实现UITableViewDataSource中的方法
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.heros.count;//设置总行数
}
//the return defaut value is 1 even the method was not overrided这里默认返回1,即使不重写此方法。
//-(NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
//{
// return 1;
//}
<span style="font-family: Arial, Helvetica, sans-serif;">//在列表中添加Cell。这里用到了一个默认格式的Cell。其他场景可以自己制作Cell,并把它添加到这里</span>
- (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];
}
//get the model of hero
Hero *hero = self.heros[indexPath.row];
cell.textLabel.text = hero.name;
cell.detailTextLabel.text = hero.intro;
cell.detailTextLabel.textColor = [UIColor orangeColor];
cell.imageView.image = [UIImage imageNamed:hero.icon];
//定义右侧小图标
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
10.在ViewController.m中添加UITableViewDelegate的几个方法实现
#pragma mark - UITableViewDelegate
//the return value is 44 even the method is commented.
//设置每个Cell的高度。默认是44像素
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 60;
}
//设置顶部statusbar隐藏
- (BOOL)prefersStatusBarHidden
{
return YES;
}
11. 完成。