ios cell自适应高度(label上字体颜色自定义)

在iOS开发当中,如果涉及到UITableViewCell的一些复杂UI的绘制时难免会碰到这么一个难题:UITableViewCell的高度如何设置!

一、cell是分组显示的,点击的时候展开/收起,直接上代码

@interface KDSXMMediaLockHelpVC ()<UITableViewDataSource,UITableViewDelegate>
{
    NSArray *dataArray;//创建一个数据源数组
    NSMutableDictionary *dic;//创建一个字典进行判断收缩还是展开
    NSMutableDictionary * rowHeighDic;
    NSArray *sectionArr;//分组的名字
}

@property (nonatomic,strong)UITableView *tableView;
///怎样配置视频锁按钮。
@property (nonatomic,strong) UIButton *configureVideoLockBtn;
///常见问题按钮。
@property (nonatomic,strong) UIButton *commonProblemBtn;
@property (nonatomic,strong) UIView * line1;
@property (nonatomic,strong) UIView * line2;
@property (nonatomic,strong) UIButton * selectedBtn;
@property (nonatomic,strong)UIImageView * arrowImg;
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KDSScreenWidth, 45)];
    view.backgroundColor = [UIColor whiteColor];
    UILabel *titleLab = [[UILabel alloc]initWithFrame:CGRectMake(20, 0, KDSScreenWidth-30, view.frame.size.height)];
    titleLab.text = sectionArr[section];
    titleLab.textColor = [UIColor blackColor];
    titleLab.font = [UIFont systemFontOfSize:15];
    titleLab.userInteractionEnabled = true;
    [view addSubview:titleLab];
    self.arrowImg = [[UIImageView alloc] initWithFrame:CGRectMake(KDSScreenWidth-30, 15, 14, 14)];
    [view addSubview:self.arrowImg];
    //创建一个手势进行点击,这里可以换成button
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(action_tap:)];
    view.tag = 300 + section;
    if (dic.allKeys.count < 1) {
        self.arrowImg.image = [UIImage imageNamed:@"icon_packup"];
    }else{
        NSString *str = [NSString stringWithFormat:@"%d",view.tag - 300];
        if ([dic[str] integerValue] == 0) {
            self.arrowImg.image = [UIImage imageNamed:@"icon_packup"];
        }else{
            self.arrowImg.image = [UIImage imageNamed:@"icon_unfold"];
        }
    }
    
    [view addGestureRecognizer:tap];
    return view;
}

- (void)action_tap:(UIGestureRecognizer *)tap{
    NSString *str = [NSString stringWithFormat:@"%d",tap.view.tag - 300];
    if ([dic[str] integerValue] == 0) {//如果是0,就把1赋给字典,打开cell
        [dic setObject:@"1" forKey:str];
    }else{//反之关闭cell
        [dic setObject:@"0" forKey:str];
    }
    [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:[str integerValue]]withRowAnimation:UITableViewRowAnimationFade];//有动画的刷新
}
//懒加载
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 54, KDSScreenWidth, KDSScreenHeight-kNavBarHeight-kBottomSafeHeight-kStatusBarHeight-44) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = UIColor.clearColor;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.estimatedRowHeight = 5000;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.tableFooterView = [[UIView alloc]init];
    }
    return _tableView;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView{
    return dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 45;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSString *string = [NSString stringWithFormat:@"%ld",(long)section];
    if ([dic[string] integerValue] == 1 ) {  //打开cell返回数组的count
        NSArray *array = [NSArray arrayWithArray:dataArray[section]];
        return array.count;
    }else{
        return 0;
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *rowStr = [NSString stringWithFormat:@"%ld",(long)indexPath.section];
    NSString * rowHeigh = rowHeighDic[rowStr];
    return rowHeigh.intValue +30;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell==nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
    }
    cell.backgroundColor = [UIColor whiteColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.text = dataArray[indexPath.section][indexPath.row];
    cell.textLabel.font = [UIFont systemFontOfSize:12];
    cell.textLabel.numberOfLines = 0;
    cell.textLabel.textColor = KDSRGBColor(102, 102, 102);
    [self setLabelSpace:cell.textLabel withSpace:10.0f withFont:[UIFont systemFontOfSize:12] section:indexPath.section];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 10.f;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView * footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KDSScreenWidth, 10)];
    footView.backgroundColor = [UIColor colorWithRed:242/255.0 green:242/255.0 blue:242/255.0 alpha:1.0];
    return footView;
}


-(void)setLabelSpace:(UILabel*)label withSpace:(CGFloat)space withFont:(UIFont*)font section:(NSInteger)section {
    
    NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
    paraStyle.lineBreakMode = NSLineBreakByCharWrapping;
    paraStyle.alignment = NSTextAlignmentLeft;
    paraStyle.lineSpacing = space; //设置行间距
    paraStyle.hyphenationFactor = 1.0;
    paraStyle.firstLineHeadIndent = 0.0;
    paraStyle.paragraphSpacingBefore = 0.0;
    paraStyle.headIndent = 0;
    paraStyle.tailIndent = 0;
    NSDictionary *attrs = @{NSFontAttributeName:[UIFont systemFontOfSize:14], NSForegroundColorAttributeName:UIColor.blackColor, NSParagraphStyleAttributeName:paraStyle};
    NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:label.text attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12], NSParagraphStyleAttributeName:paraStyle}];
    [attrStr addAttributes:attrs range:[label.text rangeOfString:@"解决方法:"]];
    label.attributedText = attrStr;
    
    CGFloat rowHeight = [self getLabelHeightWithText:label.text width:KDSScreenWidth-30 textFont:[UIFont systemFontOfSize:12] lineHeight:10];
    NSString *rowStr = [NSString stringWithFormat:@"%ld",(long)section];
    [rowHeighDic setObject:@(rowHeight) forKey:rowStr];
}

/* text 显示的字符串
   font 字体大小
   lineHeight 行间距
 */
- (CGFloat)getLabelHeightWithText:(NSString *)text width:(CGFloat)width textFont:(UIFont *)font lineHeight:(NSInteger)lineHeight{

    NSMutableParagraphStyle *paragrapStyle = [[NSMutableParagraphStyle alloc] init];
    paragrapStyle.lineSpacing = lineHeight;
    NSDictionary *attribute = @{NSFontAttributeName:font,
                                NSParagraphStyleAttributeName:paragrapStyle};
    CGRect rect = [text boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:NSStringDrawingUsesFontLeading|NSStringDrawingUsesLineFragmentOrigin attributes:attribute context:nil];
    return rect.size.height;
}

///点击开锁记录、预警信息按钮调整滚动视图的偏移,切换页面。
- (void)clickRecordBtnAdjustScrollViewContentOffset:(UIButton *)sender
{
    if (sender!= self.selectedBtn) {
        self.selectedBtn.selected = NO;
        sender.selected = YES;
        self.selectedBtn = sender;
    }else{
        self.selectedBtn.selected = YES;
    }
    if (self.selectedBtn == self.configureVideoLockBtn) {
        self.line2.hidden = YES;
        self.line1.hidden = NO;
        [self creatData];
        [self.tableView reloadData];
    }else{
        self.line2.hidden = NO;
        self.line1.hidden = YES;
        [self upDateData];
        
        [self.tableView reloadData];
    }

}

二、效果如下:

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值