uitableviewcell textlabel detailtextLabel 换行的位置及尺寸问题

本文介绍如何在iOS应用中自定义UITableViewCell样式,包括调整UILabel布局、设置checkmark颜色及根据文本内容动态计算cell高度的方法。

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

我们在使用uitableView的时候,一些简单的cell样式其实是不需要自定义的,但是系统的方法又似乎又无法满足需要,这时候我们就需要在系统上做一些改变来达到我们的需求;

像这种cell,简单分析下,一个textlabel,一个detailTextlabel,一个accview就可以实现。我们需要做的就是一些细节的处理;先上代码:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([UITableViewCell class])];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:NSStringFromClass([UITableViewCell class])];
    }
    
    cell.textLabel.textColor = TextblackColor;
    cell.textLabel.font = [UIFont systemFontOfSize:[QDFontHelp Font:14]];
    if (indexPath.row == 0) {
        textString =  @"现金分红(默认)";
        detailTextString = @"分红所得资金实时发放到钱包";
    }else{
        textString =  @"红利再投资";
        detailTextString = @"分红所得资金直接用于购买该基金,将分红转为持有基金份额";
    }

    cell.textLabel.text = textString;
    cell.detailTextLabel.text = detailTextString;
    cell.detailTextLabel.numberOfLines = 0;
    cell.detailTextLabel.font = [UIFont systemFontOfSize:[QDFontHelp Font:12]];
    [cell.textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(cell.mas_left).offset(WidthScale(15));
        make.top.equalTo(cell.mas_top).offset(HeightScale(5));
    }];
    [cell.detailTextLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(cell.mas_left).offset(WidthScale(15));
        make.right.equalTo(cell.mas_right).offset(-WidthScale(100));
        make.top.equalTo(cell.textLabel.mas_bottom);
    }];
    cell.detailTextLabel.textColor = TextGrayColor;
    if (indexPath.row == self.fenghongType) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }else{
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    cell.tintColor = APPThemeColor;
    return cell;
}

  在这里,我们对系统的textlabel和detailtextlabel进行了重新布局,已避免系统根据我们给出的cell高度进行自动适应;

关于checkmark的颜色,我们可以通过cel的tintcolor来改变!

写到这里,那么我们如何动态返回cell的高度呢,这里我们根据label的内容来返回,代码如下

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{


      CGSize size1 = [textString boundingRectWithSize:CGSizeMake(kScreen_width - WidthScale(115), 1000) 
options:NSStringDrawingUsesFontLeading|NSStringDrawingUsesLineFragmentOrigin
attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:[QDFontHelp Font:14]]} context:nil].size; CGSize size2 = [detailTextString boundingRectWithSize:CGSizeMake(kScreen_width - WidthScale(115), 1000)
options:NSStringDrawingUsesFontLeading|NSStringDrawingUsesLineFragmentOrigin
attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:[QDFontHelp Font:12]]} context:nil].size;
return size1.height + size2.height + HeightScale(15); }

  在ios7以后我们不再使用sizetofit来动态返回内容的高度  而使用boundingrect!当然这里还是有一点坑的,在具体使用的时候慢慢摸索吧!

转载于:https://www.cnblogs.com/lidarui/p/6623509.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值