【问题标题】:Calculate Cell height on basis of label text + image根据标签文字+图片计算单元格高度
【发布时间】:2015-12-03 08:10:25
【问题描述】:

我创建了一个具有 IMAGE 视图的自定义单元格,两个标签的标签数据是从 plist 文件中填充的,数据已正确填充,但在前端单元格未正确显示数据,标签剪切数据。我正在使用 Uilabel 视图。

请查看我的代码,我通过互联网搜索并遵循了一些教程,但没有任何工作。

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

    Customviewcell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell"];
    UIImage * image = [UIImage imageNamed:justThumbs[indexPath.row]];
    cell.CustomTitle.text=justTitles[indexPath.row];

    cell.CustomTitle.numberOfLines =0;
    [cell.CustomTitle sizeToFit];
    cell.CustomDes.text=justDesc[indexPath.row];
    cell.CustomDes.numberOfLines=0;
      [cell.CustomDes sizeToFit];
    [cell.CustomTitle layoutIfNeeded];
    [cell.CustomDes layoutIfNeeded];
    [cell layoutIfNeeded];
    cell.Customimage.image=image;
    return cell;
}

根据stackoverflow不同问题的答案计算高度的代码。

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //Calculate Height Based on a cell
    if (!self.customcell) {
      self.customcell=[tableView dequeueReusableCellWithIdentifier:@"cell"];
    }
    // Configure Cell
    UIImage * image = [UIImage imageNamed:justThumbs[indexPath.row]];
    self.customcell.CustomTitle.text=justTitles[indexPath.row];
    self.customcell.CustomTitle.numberOfLines=0;
    [self.customcell.CustomTitle sizeToFit];
    self.customcell.CustomDes.text=justDesc[indexPath.row];
    self.customcell.CustomDes.numberOfLines=0;
    [self.customcell.CustomDes sizeToFit];
    self.customcell.Customimage.image=image;

    //Layout Cell

    //Get Hieght for the cell

    if([[UIDevice currentDevice]userInterfaceIdiom]==UIUserInterfaceIdiomPhone)
    {

        if ([[UIScreen mainScreen] bounds].size.height == 568)
        {

            CGRect frame = [NSString setAttributeWithString:self.customcell.CustomTitle.text withLineSpacing:0.2 withSize:CGSizeMake(270, 999999999) withFont:self.customcell.CustomTitle.font withLabel:self.customcell.CustomTitle setLabelTextColor:self.customcell.CustomTitle.textColor setTextAlignment:self.customcell.CustomTitle.textAlignment];
            self.customcell.CustomTitle.height.constant = frame.size.height;
            frame = [NSString setAttributeWithString:self.customcell.CustomDes.text withLineSpacing:0.3 withSize:CGSizeMake(150, 999999999) withFont:self.customcell.CustomDes.font withLabel:self.customcell.CustomDes setLabelTextColor:self.customcell.CustomDes.textColor setTextAlignment:self.customcell.CustomDes.textAlignment];
            self.customcell.CustomDes.height.constant = frame.size.height;



        }
        else{

            CGRect frame = [NSString setAttributeWithString:self.customcell.CustomTitle.text withLineSpacing:1 withSize:CGSizeMake(337, 999999999) withFont:self.customcell.CustomTitle.font withLabel:self.customcell.CustomTitle setLabelTextColor:self.customcell.CustomTitle.textColor setTextAlignment:self.customcell.CustomTitle.textAlignment];
            self.customcell.CustomTitle.height.constant = frame.size.height;
            frame = [NSString setAttributeWithString:self.customcell.CustomDes.text withLineSpacing:1 withSize:CGSizeMake(227, 999999999) withFont:self.customcell.CustomDes.font withLabel:self.customcell.CustomDes setLabelTextColor:self.customcell.CustomDes.textColor setTextAlignment:self.customcell.CustomDes.textAlignment];
            self.customcell.CustomDes.height.constant = frame.size.height;


        }
    }
    [self.customcell layoutIfNeeded];

       // CGFloat height = self.customcell.CustomTitle.height.constant+self.customcell.CustomDes.height.constant+189;
    CGFloat height = [self.customcell systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;


    //Add padding of 1
    return height;
}

使用 Github 开源库解决了这个问题,但没有奏效。

https://github.com/punchagency/line-height-tool

问题仍然存在,标签文本被截断,内容悬挂在必填,内容在 1000 水平 + 垂直..

请帮忙..

感谢分配。

【问题讨论】:

  • 你在使用自动布局吗?
  • @MGP 是的,我正在使用自动布局...
  • @user3015451 图片高度是固定的还是动态的?

标签: ios objective-c uitableview


【解决方案1】:

你可以通过NSString方法boundingRectWithSize来获取某个范围内字符串的高度,比如

NSString* text = @"Test text";

CGRect textRect = [text boundingRectWithSize:CGSizeMake(300, CGFLOAT_MAX)
                                                     options:NSStringDrawingUsesLineFragmentOrigin
                                                  attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:12]}
                                                     context:nil];
CGFloat textHeight = ceilf(textRect.size.height);

使用您自己的字体和字号,如果需要,您还可以在属性字典中添加其他属性。

【讨论】:

  • 这就是为什么在 heightForCellAtIndexPath 方法中对每个单元格执行此操作的原因 - 在单元格上定义一个静态方法,该方法获取单元格中显示的宽度和对象,并根据对象的内容。
【解决方案2】:

使用以下方法计算您的 UILabel 高度:

在你的类中添加也使用这个方法。

- (CGFloat)getLabelHeight:(UILabel*)label{
CGSize constraint = CGSizeMake(label.frame.size.width, CGFLOAT_MAX);
CGSize size;

NSStringDrawingContext *context = [[NSStringDrawingContext alloc] init];
CGSize boundingBox = [label.text boundingRectWithSize:constraint
                                              options:NSStringDrawingUsesLineFragmentOrigin
                                           attributes:@{NSFontAttributeName:label.font}
                                              context:context].size;

size = CGSizeMake(ceil(boundingBox.width), ceil(boundingBox.height));

return size.height;}

根据您的要求更新您的 heightForRowAtIndexPath 方法: 计算所有 UI 高度并返回。

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

Customviewcell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell"];

CGFloat totalHeight = 0;

cell.CustomTitle.text=justTitles[indexPath.row];
//get label Height
totalHeight += [Helper getLabelHeight:cell.CustomTitle];

cell.Customimage.image = [UIImage imageNamed:justThumbs[indexPath.row]];
CGFloat imageHeight = cell.Customimage.frame.size.height; //or Add image height here

totalHeight += imageHeight;

return totalHeight;}

【讨论】:

    【解决方案3】:

    在 iOS8+ 中,您可以使用自定义大小的单元格: http://www.appcoda.com/self-sizing-cells/

    【讨论】:

      【解决方案4】:

      单元格的自动调整大小在 iOS 8.0 中可用,我的部署目标是 ios 7.0,这导致了布局问题。

      请参考这些文章:

      http://www.appcoda.com/self-sizing-cells/

      代码很快,但也需要在目标 c 中做同样的事情。

      这对你也有帮助。

      http://useyourloaf.com/blog/self-sizing-table-view-cells.html

      【讨论】:

        猜你喜欢
        • 2011-04-21
        • 1970-01-01
        • 1970-01-01
        • 2013-07-25
        • 2022-12-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多