最终效果:
个人步骤:
1.导入素材
2.根据plist创建模型
在模型中添加初始化方法
懒加载获取数据
3.设置UITableView的数据源对象
遵守数据源协议,实现数据源方法
模型类
#import <UIKit/UIKit.h>
@interface YZHeros : UIView
@property (nonatomic,copy) NSString *name;
@property (nonatomic,copy) NSString *intro;
@property (nonatomic,copy) NSString *icon;
- (instancetype) initWithDict:(NSDictionary *)dict;
+ (instancetype) heroWithDict:(NSDictionary *)dict;
@end
#import "YZHeros.h"
@implementation YZHeros
- (instancetype) initWithDict:(NSDictionary *)dict{
if (self = [super init]) {
[self setValuesForKeysWithDictionary:dict];
}
return self;
}
+ (instancetype) heroWithDict:(NSDictionary *)dict{
return [[self alloc] initWithDict:dict];
}
@end
ViewController类
#import "ViewController.h"
#import "YZHeros.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic,strong) NSArray *heros;
@end
@implementation ViewController
#pragma mark 懒加载
-(NSArray *)heros{
if (_heros == nil) {
NSString *path = [[NSBundle mainBundle] pathForResource:@"heros.plist" ofType:nil];
NSArray *arrDict = [NSArray arrayWithContentsOfFile:path];
NSMutableArray *arrModel = [[NSMutableArray alloc] init];
for (NSDictionary *dict in arrDict) {
YZHeros *model = [YZHeros heroWithDict:dict];
[arrModel addObject:model];
}
_heros = arrModel;
}
return _heros;
}
#pragma mark 实现每组几行方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.heros.count;
}
#pragma mark 设置内容方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//1.获取数据
YZHeros *model = self.heros[indexPath.row];
//2.添加UITableViewCell
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];
//3.填充数据
cell.imageView.image = [UIImage imageNamed:model.icon];
cell.textLabel.text = model.name;
cell.detailTextLabel.text = model.intro;
//单元格右边显示小箭头
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
//4.返回UITableViewCell
return cell;
}
#pragma mark 隐藏、显示状态栏方法
-(BOOL)prefersStatusBarHidden{
return YES;
}
- (void)viewDidLoad {
[super viewDidLoad];
//设置单元格行高
//这是设置设置行高一样的方法
//self.tableView.rowHeight = 80;
//对于不同行高,通过代理实现
self.tableView.delegate = self;
}
#pragma mark 实现代理方法设置行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.row % 2 == 0) {
return 50;
}else{
return 80;
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end