zoukankan      html  css  js  c++  java
  • uitableviewcell textlabel detailtextLabel 换行的位置及尺寸问题

    我们在使用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!当然这里还是有一点坑的,在具体使用的时候慢慢摸索吧!

  • 相关阅读:
    RefreshParenwin 打开子窗体 并回发
    DataAdapter插入数据
    GridViewTotal
    滚动截图软件
    十个随机数排列(vb代码)
    SQL查询效率100w数据查询只要1秒
    ASP.NET操纵EXCEL导入SQL SERVER数据库的代码
    ajax blogs !good!
    轻松掌握Ajax.net系列教程
    asp.net 性能优化 Write less, do more...
  • 原文地址:https://www.cnblogs.com/lidarui/p/6623509.html
Copyright © 2011-2022 走看看