我们先是创建了一个plist文件用来保存本地的数据
//定义属性头像图片、名字、内容、图片、高度
@property (nonatomic,strong)NSString *headerImg,*name,*text,*img;
@property (nonatomic,assign)float height;
重写tableView
·//定义属性头像视图、名字标签、内容标签、图片视图
@property (nonatomic,strong)UIImageView *headerImg,*img;
@property (nonatomic,strong)UILabel *nameLab,*textLab;
。m
//初始化头像视图
- (UIImageView *)headerImg
{
if (!_headerImg) {
_headerImg = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 60, 60)];
_headerImg.layer.cornerRadius = 30;
_headerImg.layer.masksToBounds = YES;
[self addSubview:_headerImg];
}
return _headerImg;
}
//初始化名字标签
- (UILabel *)nameLab
{
if (!_nameLab) {
_nameLab = [[UILabel alloc]initWithFrame:CGRectMake(80, 20, 200, 40)];
[self addSubview:_nameLab];
}
return _nameLab;
}
//初始化内容标签
- (UILabel *)textLab
{
if (!_textLab) {
_textLab = [[UILabel alloc]init];
_textLab.numberOfLines = 0;
_textLab.font = [UIFont systemFontOfSize:15];
[self addSubview:_textLab];
}
return _textLab;
}
//初始化图片视图
- (UIImageView *)img
{
if (!_img) {
_img = [[UIImageView alloc]init];
[self addSubview:_img];
}
return _img;
}
ViewController的实现方法
#import "MyTableViewCell.h"
#import "Model.h"
<UITableViewDataSource,UITableViewDelegate>{
//定义数组、表格
NSMutableArray *arr;
UITableView *table;
}
//获取plist初始化数组
NSString *path = [[NSBundle mainBundle]pathForResource:@"user" ofType:@"plist"];
NSArray *dataArr = [NSArray arrayWithContentsOfFile:path];
//变量数组初始化Model对象
arr = [NSMutableArray array];
for (NSDictionary *dic in dataArr) {
Model *m = [[Model alloc]init];
m.headerImg = dic[@"headerImg"];
m.name = dic[@"name"];
m.text = dic[@"text"];
m.img = dic[@"img"];
//根据文字计算标签高度
float height = [dic[@"text"] boundingRectWithSize:CGSizeMake(400, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:15],NSFontAttributeName, nil] context:nil].size.height;
m.height = height;
[arr addObject:m];
}
//初始化表格
table = [[UITableView alloc]initWithFrame:self.view.bounds];
table.dataSource = self;
table.delegate = self;
[self.view addSubview:table];
//创建头部图片视图
UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200)];
img.image = [UIImage imageNamed:@"headerImage1.jpg"];
table.tableHeaderView = img;
//设置行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return arr.count;
}
//设置单元格内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellid = @"cellid";
MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];
if (!cell) {
cell = [[MyTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid];
}
//获取字典
Model *m = arr[indexPath.row];
//设置头像
cell.headerImg.image = [UIImage imageNamed:m.headerImg];
//设置名字
cell.nameLab.text = m.name;
//设置内容
cell.textLab.text = m.text;
cell.textLab.frame = CGRectMake(10, 80, 400, m.height);
//设置图片
cell.img.image = [UIImage imageNamed:m.img];
cell.img.frame = CGRectMake(100, m.height + 90, 200, 200);
return cell;
}
//设置单元格高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
Model *m = arr[indexPath.row];
//根据有无图片设置单元格高度
if ([m.img isEqualToString:@""]) {
return m.height + 90;
}else{
return m.height + 300;
}
}
//表格下拉响应方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGFloat width = self.view.frame.size.width;
// 图片宽度
CGFloat yOffset = scrollView.contentOffset.y;
// 偏移的y值
if(yOffset < 0)
{CGFloat totalOffset = 200 + ABS(yOffset);
CGFloat f = totalOffset / 200;
//拉伸后的图片的frame应该是同比例缩放。
table.tableHeaderView.frame = CGRectMake(- (width *f-width) / 2, yOffset, width * f, totalOffset);
}
}