iOS 表格头部视图下拉放大+单元格重写图文混排

效果展示

QQ20170925-155107-HD.gif

ViewController.m

#import "ViewController.h"
#import "Model.h"
#import "FrameModel.h"
#import "TableViewCell.h"
@property(nonatomic,strong)UIImageView *headImageView;//头部图片
@property(nonatomic,strong)UITableView *tableView;//列表
@property (nonatomic, strong) NSArray *InfoArray;//数组
//屏幕宽、高 宏定义
#define IPHONE_W ([UIScreen mainScreen].bounds.size.width)
#define IPHONE_H ([UIScreen mainScreen].bounds.size.height)
// 头视图高度
static CGFloat kImageOriginHight = 250;
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    //将视图添加到界面上
    [self.view addSubview:self.tableView];
    [self.tableView addSubview:self.headImageView];
    
    //隐藏系统tableViewCell分割线
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    //隐藏垂直滚动条
    self.tableView.showsVerticalScrollIndicator = NO;
    
    [self getInfo];
}
#pragma mark -- 滚动视图的代理方法
- (void)scrollViewDidScroll:(UIScrollView*)scrollView{
    /**
     *  关键处理:通过滚动视图获取到滚动偏移量从而去改变图片的变化
     */
    //获取滚动视图y值的偏移量
    CGFloat yOffset  = scrollView.contentOffset.y;
    NSLog(@"yOffset===%f",yOffset);
    CGFloat xOffset = (yOffset +kImageOriginHight)/2;
    
    if(yOffset < -kImageOriginHight) {
        CGRect f =self.headImageView.frame;
        f.origin.y= yOffset ;
        f.size.height=  -yOffset;
        f.origin.x= xOffset;
        //int abs(int i); // 处理int类型的取绝对值
        //double fabs(double i); //处理double类型的取绝对值
        //float fabsf(float i); //处理float类型的取绝对值
        f.size.width=IPHONE_W + fabs(xOffset)*2;
        
        self.headImageView.frame= f;
    }
}
#pragma mark -- 表视图代理
- (void)getInfo
{
    //实际开发数据是网络获取到的,这里模拟给出一个数据
    NSArray *array = @[
                       @{@"name" : @"aaa", @"icon" : @"icon", @"text" : @"这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容", @"picture" : @"hero.jpg"},
                       @{@"name" : @"bbb", @"icon" : @"icon", @"text" : @"这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容", @"picture" : @"hero.jpg"},
                       @{@"name" : @"ccccc", @"icon" : @"icon", @"text" : @"这里是内容,没有配图"},
                       @{@"name" : @"ddd", @"icon" : @"icon", @"picture" : @"hero.jpg"}];
    
    //解析数据,转模型保存
    NSMutableArray *tempArray = [NSMutableArray array];
    for (NSDictionary *dict in array) {
        Model *model = [Model modelWithDict:dict];
        FrameModel *frameModel = [[FrameModel alloc] init];
        frameModel.model = model;
        [tempArray addObject:frameModel];
    }
    self.InfoArray = [tempArray copy];
    
    NSLog(@"%ld",_InfoArray.count);
}

#pragma mark - Table view data source
//组数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

//组中行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.InfoArray.count;
}

//cell内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TableViewCell *cell = [TableViewCell cellWIthTableView:tableView];
    
    cell.frameModel = self.InfoArray[indexPath.row];
    
    return cell;
}

//设置行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FrameModel *frameModel = self.InfoArray[indexPath.row];
    
    return frameModel.cellHeight;
}
#pragma mark -- get 初始化操作

-(UITableView *)tableView
{
    if (_tableView == nil)
    {
        _tableView= [[UITableView alloc]initWithFrame:CGRectMake(0,0,IPHONE_W,IPHONE_H)];
        _tableView.delegate=self;
        _tableView.dataSource=self;
        _tableView.backgroundColor= [UIColor lightGrayColor];
        //内容由kImageOriginHight 处开始显示。
        _tableView.contentInset=UIEdgeInsetsMake(kImageOriginHight,0,0,0);
    }
    return _tableView;
}

-(UIImageView *)headImageView
{
    if (_headImageView == nil)
    {
        _headImageView= [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"111.jpg"]];
        _headImageView.frame=CGRectMake(0, -kImageOriginHight,IPHONE_W,kImageOriginHight);
    }
    return _headImageView;
}

Model.h

#import <Foundation/Foundation.h>

@interface Model : NSObject

@property (nonatomic, copy) NSString *icon;
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *text;
@property (nonatomic, copy) NSString *picture;

- (id)initWithDict:(NSDictionary *)dict;
+ (id)modelWithDict:(NSDictionary *)dict;

@end

Model.m

#import "Model.h"

@implementation Model

- (id)initWithDict:(NSDictionary *)dict
{
    self = [super init];
    if (self) {
        [self setValuesForKeysWithDictionary:dict];
    }
    return self;
}

+ (id)modelWithDict:(NSDictionary *)dict
{
    return [[self alloc] initWithDict:dict];
}

@end

FrameModel.h

@class Model;

@interface FrameModel : NSObject

@property (nonatomic, assign) CGRect iconFrame;
@property (nonatomic, assign) CGRect nameFrame;
@property (nonatomic, assign) CGRect textFrame;
@property (nonatomic, assign) CGRect pictureFrame;
@property (nonatomic, assign) CGFloat cellHeight;
@property (nonatomic, strong) Model *model;

@end

FrameModel.m

#import "FrameModel.h"

#import "Model.h"

#define mainW [UIScreen mainScreen].bounds.size.width
#define HWTextFont [UIFont systemFontOfSize:15]

@implementation FrameModel

- (void)setModel:(Model *)model
{
    _model = model;
    
    //头像
    CGFloat padding = 10;
    CGFloat iconWH = 30;
    self.iconFrame = CGRectMake(padding, padding, iconWH, iconWH);
    
    //名字
    CGSize nameSize = [self sizeWithText:model.name font:HWTextFont maxSize:CGSizeMake(MAXFLOAT, MAXFLOAT)];
    CGFloat nameW = nameSize.width;
    CGFloat nameH = nameSize.height;
    CGFloat nameX = CGRectGetMaxX(self.iconFrame) + padding;
    CGFloat nameY = CGRectGetMinY(self.iconFrame);
    self.nameFrame = CGRectMake(nameX, nameY, nameW, nameH);
    
    //文字内容
    CGSize textSize = [self sizeWithText:model.text font:HWTextFont maxSize:CGSizeMake(mainW - padding * 2, MAXFLOAT)];
    self.textFrame = CGRectMake(padding, iconWH + padding * 2, textSize.width, textSize.height);
    
    //配图
    if (model.picture) {
        self.pictureFrame = CGRectMake(padding, CGRectGetMaxY(self.textFrame) + padding, 120, 120);
        _cellHeight = CGRectGetMaxY(self.pictureFrame) + padding;
    }
    else {
        _cellHeight = CGRectGetMaxY(self.textFrame) + padding;
    }
}

//根据字体大小、限定长度动态获取文字宽高尺寸
- (CGSize)sizeWithText:(NSString *)text font:(UIFont *)font maxSize:(CGSize)maxSize
{
    NSDictionary *dict = @{NSFontAttributeName : font};
    return [text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil].size;
}

TableViewCell.h

#import <UIKit/UIKit.h>

@class FrameModel;

@interface TableViewCell : UITableViewCell

@property (nonatomic, strong) FrameModel *frameModel;

+ (instancetype)cellWIthTableView:(UITableView *)tableView;

@end

TableViewCell.m

#import "TableViewCell.h"
#import "Model.h"
#import "FrameModel.h"

#define HWTextFont [UIFont systemFontOfSize:15]

@interface TableViewCell ()

@property (nonatomic, weak) UIImageView *icon;
@property (nonatomic, weak) UILabel *name;
@property (nonatomic, weak) UILabel *text;
@property (nonatomic, assign) UIImageView *picture;

@end

@implementation TableViewCell
+ (instancetype)cellWIthTableView:(UITableView *)tableView
{
    //cell复用,唯一标识
    static NSString *identifier = @"HWCell";
    //先在缓存池中取
    TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    //缓存池中没有再创建,并添加标识,cell移出屏幕时放入缓存池以复用
    if (cell == nil) {
        cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    
    return cell;
}

//重写init方法构建cell内容
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        //取消点击高亮状态
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        //头像
        UIImageView *icon = [[UIImageView alloc] init];
        [self.contentView addSubview:icon];
        self.icon = icon;
        
        //名字
        UILabel *name = [[UILabel alloc] init];
        name.font = HWTextFont;
        [self.contentView addSubview:name];
        self.name = name;
        
        //内容
        UILabel *text = [[UILabel alloc] init];
        text.numberOfLines = 0;
        text.font = HWTextFont;
        [self.contentView addSubview:text];
        self.text = text;
        
        //配图
        UIImageView *picture = [[UIImageView alloc] init];
        [self.contentView addSubview:picture];
        self.picture = picture;
    }
    
    return self;
}

//重写set方法,模型传递
- (void)setFrameModel:(FrameModel *)frameModel
{
    _frameModel = frameModel;
    
    Model *model = frameModel.model;
    
    self.icon.image = [UIImage imageNamed:model.icon];
    self.icon.frame = frameModel.iconFrame;
    
    self.name.text = model.name;
    self.name.frame = frameModel.nameFrame;
    
    self.text.text = model.text;
    self.text.frame = frameModel.textFrame;
    
    self.picture.image = [UIImage imageNamed:model.picture];
    self.picture.frame = frameModel.pictureFrame;
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,242评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,769评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,484评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,133评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,007评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,080评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,496评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,190评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,464评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,549评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,330评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,205评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,567评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,889评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,160评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,475评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,650评论 2 335

推荐阅读更多精彩内容

  • 前言 最近忙完项目比较闲,想写一篇博客来分享一些自学iOS的心得体会,希望对迷茫的你有所帮助。博主非科班出身,一些...
    GitHubPorter阅读 1,407评论 9 5
  • iOS网络架构讨论梳理整理中。。。 其实如果没有APIManager这一层是没法使用delegate的,毕竟多个单...
    yhtang阅读 5,144评论 1 23
  • NO.1 曾经的她,像个快乐的天使。有着银铃般的笑声,只要她一开口,我们都忍不住说: 小喇叭又开始广播啦! 不知道...
    阳光Sunflower阅读 2,250评论 0 1
  • 我喜欢写云,你看 今天的云端又多了几座庙宇 倒映在水中的层层石阶 缓缓而上 满眼的落叶和荒芜 是我曾经喜爱的地方 ...
    陶墨墨阅读 842评论 8 2