我们在这实现一个类似于朋友圈的一种图片下拉刷新的功能,在实现此功能前我们需要借助一个第三方文件 stretchTableHeader
将它导入我们的工程中并且导入两张图片方便后续使用, 接下来就书写我们的代码
首先我们要创建一个plist文件,可根据兴趣自行书写内容
接着 我们创建一个继承于 UITableViewCell 的文件
在.h文件中声明属性
@property(nonatomic,strong)UILabel *lab1,*lab2;
@property(nonatomic,strong)UIImageView *img1,*img2;
在.m文件中实现具体的代码,此处坐标需要自行调整;
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
{
[self addSubview:self.theL1];
[self addSubview:self.theL2];
[self addSubview:self.theImage];
}
return self;
}
- (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;
}
回到ViewController.m中继续书写
#import "ViewController.h"
#import "MyTableViewCell.h"
#import "HFStretchableTableHeaderView.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
{
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];
}