TableView表头展开收起

有时候可能文本内容有些多,需要折起来让界面看着不那么臃肿,就可能会先将内容折起来,点击按钮的时候,才会扩展开,那么我们今天就做一个这个的Demo。
先自定义一个视图,将会放在表头上面:

@interface HTIndustryDetailHeadView()
/**
 *  简介文字
 */
@property (nonatomic, strong) UILabel  *introductionLabel;
/**
 *  展开按钮
 */
@property (nonatomic, strong) UIButton *anButton;
@end
@implementation HTIndustryDetailHeadView
 
- (instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    
    if (self) {
        
        self.backgroundColor = [UIColor redColor];
        
        _introductionLabel = [[UILabel alloc]init];
        _anButton              = [UIButton buttonWithType:UIButtonTypeCustom];
        
        _introductionLabel.font = [UIFont systemFontOfSize:15];
        _introductionLabel.numberOfLines = 0;
        _introductionLabel.textColor = [UIColor blackColor];
        
        [_anButton setTitle:@"展开" forState:UIControlStateNormal];
        [_anButton addTarget:self action:@selector(anButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        
        _anButton.titleLabel.textColor = GRAY_SYSTEM_COLOR;
        _anButton.titleLabel.font = [UIFont systemFontOfSize:15];
        _anButton.hidden = YES;
        _anButton.backgroundColor = [UIColor blackColor];
        
        [self addSubview:_introductionLabel];
        [self addSubview:_anButton];
    }
    
    return self;
}
- (void)setCourseIntroduction:(NSString *)courseIntroduction{
    _courseIntroduction = courseIntroduction;
    
    _introductionLabel.text = courseIntroduction;
    
    UIFont *font = [UIFont systemFontOfSize:15];
    
    CGSize introducitonSize = [_courseIntroduction sizeWithFont:font maxSize:CGSizeMake(WIDTH - 20, 999)];
    /**
     *  如果文字高度超过了70,那么就显示折起按钮
     */
    if (introducitonSize.height > 70) {
        _introductionLabel.frame = CGRectMake(5, 5, WIDTH - 10, 80);
        _anButton.hidden = NO;
        _anButton.frame = CGRectMake(5, 90, WIDTH - 10, 20);
        
    }else{
        _introductionLabel.frame = CGRectMake(5, 5, WIDTH - 10, introducitonSize.height);
        _anButton.hidden = YES;
    }

}
/**
 *  折起按钮响应事件
 */
- (void)anButtonClick:(UIButton *)btn{

    UIFont *font = [UIFont systemFontOfSize:15];
    
    CGSize introducitonSize = [_courseIntroduction sizeWithFont:font maxSize:CGSizeMake(WIDTH - 10, 999)];
   
    if (!btn.selected) {
        _introductionLabel.frame = CGRectMake(5, 5, WIDTH - 10, introducitonSize.height);
        _anButton.hidden = NO;
        _anButton.frame  = CGRectMake(5, introducitonSize.height + 5, WIDTH - 10, 20);
        [_anButton setTitle:@"收起" forState:UIControlStateNormal];
    }else{ 
        _introductionLabel.frame = CGRectMake(5, 5, WIDTH - 10, 80);
        _anButton.hidden = NO;
        _anButton.frame  = CGRectMake(5, 90, WIDTH - 10, 20);
        [_anButton setTitle:@"展开" forState:UIControlStateNormal];
    
    }
    /**
     *  将文本的高度发送给tableView控制器
     */
    NSNumber *introductionHeight = [NSNumber numberWithFloat:_introductionLabel.frame.size.height]; 
    btn.selected = !btn.selected;
    
    NSDictionary *dic = @{@"introductionHeight":introductionHeight};
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"DID_CLICK_ANBUTTON" object:nil userInfo:dic];
}

我们在计算label的高度的时候用到了一个方法:

- (CGSize)sizeWithFont:(UIFont *)font maxSize:(CGSize)maxSize
{
//返回的是计算好的高度
    NSDictionary *attrs = @{NSFontAttributeName : font};
    return [self boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
}

我们将此View放置为表头,根据点击通知来实现frame的切换:

  _tableView = [[UITableView alloc]init];
    _tableView.frame = CGRectMake(0, 0, WIDTH, HEIGHT);
    
    _tableView.dataSource = self;
    _tableView.delegate   = self;
    
    _headView = [[HTIndustryDetailHeadView alloc]init];
//默认先讲表头高度设置为120
    _headView.frame = CGRectMake(0, 0, WIDTH, 120);
    _headView.courseIntroduction = @"优先加载文本内容,其次加载课程图片加载图片前增加占位图,即框架固定优先加载文本内容,其次加载课程图片加载图片前增加占位图,即框架固定优先加载文本内容,其次加载课程图片加载图片前优先加载文本内容,其次加载课程图片加载图片前增加占位图,即框架固定优先加载文本内容,其次加载课程图片加载图片前增加占位图,即框架固定优先加载文本内容,其次加载课程图片加载图片前增加占位图,即框架固定优先加载文本内容,其次加载课程图片加载图片前增加占位图,即框架固定优先加载文本内容,其次加载课程图片加载图片前增加占位图,即框架固定";
    
    _tableView.tableHeaderView = _headView; 
    [self.view addSubview:_tableView];
    [self setExtraCellLineHidden:_tableView];
/**
 *  通知方法,刷新表头
 */
- (void)updetaUI:(NSNotification *) noti{

    
    NSDictionary *dic   = noti.userInfo;
    CGRect rect         = _headView.frame;
    rect.size.height    = [dic[@"introductionHeight"] floatValue] + 35;
    /**
     *  从新设置表头frame
     */
    _headView.frame     = rect;
    self.tableView.tableHeaderView = _headView;
}

我们看下效果,展开前:

IMG_4730.jpg

收起:


IMG_4731.jpg

好了, 大家可以尝试着实现更多的展开收起效果!

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

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 11,646评论 4 59
  • 规则要求: tableview 有多层,类似于xcode文件目录的层级关系,每一个最开始展示的层姑且称之为根目录吧...
    34码的小孩子阅读 3,177评论 9 8
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 170,598评论 25 707
  • 昨晚一岁说让我今天和闺女一起去体检,昨晚洗过澡之后就去了她家。 今天早上六点半就起床准备去妇幼保健院。幸好挺健康。...
    傅五岁阅读 139评论 0 0
  • 晨风习习 吹皱一池春水 湖边蜿蜒小径 三三两两早起的人 或细语呢喃 或轻步健走 我 独坐长椅之上 满眼春色 满心欢喜
    爱上咖啡的鱼阅读 160评论 0 1