Oc 下拉放大表格stretchTableHeader

本文介绍如何在iOS应用中使用Plist文件加载数据,并展示如何将这些数据应用于UITableView的显示,包括自定义UITableViewCell和实现拉伸头部效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Plist文件:
friends.plist

屏幕快照 2017-09-11 08.11.06.png

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];
}
@end

MyTableViewCell.h


#import <UIKit/UIKit.h>

@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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值