标签尺寸适合UITableView

时间:2014-05-14 10:46:06

标签: ios objective-c uitableview uilabel sizetofit

sizeToFit的{​​{1}}方法不适用于UILabel

问题是我需要一些我的单元格看起来像这样: enter image description here

和其他人看起来像这样: enter image description here

这是什么解决方案?

由于

2 个答案:

答案 0 :(得分:1)

sizeToFit方法在故事板中禁用自动布局后执行在cellForRowAtIndexPath方法中应该执行的操作。

答案 1 :(得分:0)

我遇到了同样的问题,我解决了这个问题:

-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIFont *font = [UIFont fontWithName:@"Roboto-Regular" size:14];
    CGSize textSize = [[eventDetails objectForKey:[detailTitle objectAtIndex: indexPath.row]] sizeWithFont:font];
    CGFloat strikeWidth = textSize.width;
    if(strikeWidth <280) // if the length is lower than max width
    {
        return  30; // normal height of cell
    }
    else
    {
        return 30 + ((int) strikeWidth/280 )*15;// if not give the cell the height you want
    }
}

不要忘记将标签行数设置为0 [label setNumberOfLines:0];

相关问题