//-----------------------------ViewController.m----------------------
#import "ViewController.h"#import "MyTableViewCell.h"#import "HFStretchableTableHeaderView.h"@interface ViewController (){
UITableView *_tableView;
NSMutableArray *dataArr;
}
@property(nonatomic,strong)NSDictionary *dic;
@property (nonatomic,strong)HFStretchableTableHeaderView *stretchHeaderView;
#define StretchHeaderHeight 200
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 初始化表格
_tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) style:UITableViewStylePlain];
// 设置代理
_tableView.delegate = self;
_tableView.dataSource = self;
// 添加到视图上
[self.view addSubview:_tableView];
[self initStretchHeader];
NSString *plistPath = [[NSBundle mainBundle]pathForResource:@"friends" ofType:@"plist"];
dataArr = [[NSMutableArray alloc]initWithContentsOfFile:plistPath];
NSLog(@"%@",dataArr);//直接打印数据
}
- (void)initStretchHeader
{
//背景
UIImageView *bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, StretchHeaderHeight)];
bgImageView.contentMode = UIViewContentModeScaleAspectFill;
bgImageView.clipsToBounds = YES;
bgImageView.image = [UIImage imageNamed:@"headerImage1.jpg"];
//背景之上的内容
UIView *contentView = [[UIView alloc] initWithFrame:bgImageView.bounds];
contentView.backgroundColor = [UIColor clearColor];
self.stretchHeaderView = [HFStretchableTableHeaderView new];
[self.stretchHeaderView stretchHeaderForTableView:_tableView withView:bgImageView subViews:contentView];
}
#pragma -
#pragma mark - UITableViewDelegate表格的代理
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 200;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return dataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"cell";
MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (!cell) {
cell = [[MyTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
}
NSDictionary *dic = dataArr[indexPath.row];
[dic objectForKey:@"content"];
NSLog(@"________%@",dic);
cell.lab1.text = [dic objectForKey:@"nickname"];
cell.lab2.text = [dic objectForKey:@"content"];
cell.img1.image = [UIImage imageNamed:[dic objectForKey:@"headImg"]];
cell.img2.image = [UIImage imageNamed:[dic objectForKey:@"headImg"]];
if (indexPath.row == 0) {
cell.img2.image = nil;
}
return cell;
}
#pragma mark - stretchableTable delegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
[self.stretchHeaderView scrollViewDidScroll:scrollView];
}
- (void)viewDidLayoutSubviews
{
[self.stretchHeaderView resizeView];
}
@end
//-----------------MyTableViewCell.h------------------------
#import@interface MyTableViewCell : UITableViewCell
@property(nonatomic,strong)UILabel *lab1,*lab2;
@property(nonatomic,strong)UIImageView *img1,*img2;
@end
//-----------------MyTableViewCell.m-----------------------
#import "MyTableViewCell.h"
@implementation MyTableViewCell
- (UILabel *)lab1{
if (!_lab1) {
_lab1 = [[UILabel alloc]initWithFrame:CGRectMake(60, 22, 200, 12)];
[self addSubview:_lab1];
}
return _lab1;
}
- (UILabel *)lab2{
if (!_lab2) {
_lab2 = [[UILabel alloc]initWithFrame:CGRectMake(10, 72, 400, 24)];
[self addSubview:_lab2];
}
return _lab2;
}
- (UIImageView *)img1{
if (!_img1) {
_img1 = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 40, 40)];
[_img1.layer setCornerRadius:40/2];
_img1.layer.masksToBounds = YES ;
[self addSubview:_img1];
}
return _img1;
}
- (UIImageView *)img2{
if (!_img2) {
_img2 = [[UIImageView alloc]initWithFrame:CGRectMake(100, 112, 200, 80)];
[self addSubview:_img2];
}
return _img2;
}
@end
网友评论