【问题标题】:Height issues with UILabel in UITableViewCellUITableViewCell 中 UILabel 的高度问题
【发布时间】:2014-05-30 10:12:52
【问题描述】:

我在 UITableViewCell 中的 UILabel 遇到了一些麻烦,它很好地扩展了它的高度。但是当单元格被重用并且 UILabel 文本发生变化时,当我向下滚动时,向上滚动具有正确高度的传递标签,现在是错误的。

据我所知,这是 UILabel 高度的问题,但我不明白什么以及为什么会发生。

这是发生的视频:

您可以看到图像下方表格单元格中的标签看起来不错,但是当我向上滚动时,它们突然只有两行,而不是四行。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    InstagramMedia *media = mediaArray[indexPath.section];

    CGSize maximumLabelSize = CGSizeMake(304.0f, 20000.0f);
    CGSize expectedLabelSize = [media.caption.text sizeWithFont:[UIFont systemFontOfSize:13.0f] constrainedToSize:maximumLabelSize lineBreakMode:NSLineBreakByWordWrapping];

    return (320.0f + expectedLabelSize.height + 20.0f);
}

我错过了什么吗?

【问题讨论】:

  • 标签的大小是应该的还是比预期的小?
  • @wootage 起初它应该是这样,但是当上下滚动时,它会变成错误的大小。
  • 您是否尝试为标签和单元格背景设置不同的颜色以查看实际大小?我猜您有表格单元格的自定义类,在该类中实现 - (void)layoutSubviews 并设置标签大小那里
  • @wootage 似乎单元格在所有单元格上都有正确的高度。麻烦的是UILabel。上下滚动时,它的高度似乎是随机选择的。其中一些在向下滚动时是正确的,但是当我向上滚动时,UILabel 高度是错误的。
  • 那么单元格类中的layoutSubviews 应该可以完成您的工作。只需在其中设置标签框

标签: ios uitableview uilabel


【解决方案1】:

我尝试使用稍微不同的方法,它在模拟器中工作。单元格在滚动时保持其高度。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    InstagramMedia *media = mediaArray[indexPath.section];

    NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"HelveticaNeue" size:12], NSFonatAttributeName, nil];
    NSAttributedString *textStr = [[NSAttrebutedString alloc]initWithString:media.caption.text attributes:attributes];
    CGSize maximumLabelSize = CGSizeMake(304.0f, 20000.0f);
    CGRect expLblRect = [textStr boundingRectWithSize:maximumLabelSize options:NSStringDrawingUsesLineFragmentOrigin context:nil];

    return (320.0f + expLblFrame.size.height + 20.0f);
}

【讨论】:

    【解决方案2】:

    试试这个:

    - (CGFloat)tableView:(UITableView *)_tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    
        UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 186, 0)]; //max width that is allowed for label
        lbl.numberOfLines = 0;
        lbl.lineBreakMode = NSLineBreakByWordWrapping;
    
        int GAP = 3;
        float height = 0;
        float fontSize = 14;
        NSString *cellText = yourtext;
        lbl.font= [ UIFont fontWithName:FONT_NAME_Ubuntu size: fontSize];
        bl.text = cellText;
        [lbl sizeToFit];
        height += lbl.frame.size.height;
        height += GAP;
    
        return height;
      }         
    

    或者你可以试试这个:

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    
            /*
            for dynamic cell heigh according to the description display in cell
             */
    
            NSDictionary *dict1 = (NSDictionary*)[array objectAtIndex:indexPath.row];
            NSString *cellText = [dict1 valueForKey:@"test"];
            UIFont *cellFont = [UIFont fontWithName:FONT_NAME_GOTHAM_4 size:11.0];
            CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);
            CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:NSLineBreakByWordWrapping];
    
    
                return labelSize.height + 80;
    
    
    }
    

    【讨论】:

    • 第一个不起作用,第二个和我的代码一样,if 条件专家。为什么我需要检查 iOS 7?
    • 对不起我在我的代码中使用了这个条件。所以不要接受那个条件。但是第二个对我有用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-06
    • 2018-08-28
    • 2014-12-15
    • 1970-01-01
    • 1970-01-01
    • 2010-11-03
    相关资源
    最近更新 更多