【问题标题】:Label Size to Fit inside UITableView标签大小以适合 UITableView
【发布时间】:2014-07-02 09:04:07
【问题描述】:

UILabelsizeToFit 方法不适用于cellForRowAtIndexPath

问题是我需要一些细胞看起来像这样:

和其他人看起来像这样:

解决办法是什么?

谢谢

【问题讨论】:

  • 使用 [urlabelname sizetofit];
  • 试试 willDisplayCell:forRowAtIndexPath: 而不是 cellForRowAtIndexPath
  • 尝试改变UILabel的行数。或者你可以做的是创建两个单独的自定义 UITableViewCells。

标签: ios objective-c uitableview uilabel sizetofit


【解决方案1】:

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

-(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];

【讨论】:

    【解决方案2】:

    在情节提要中禁用自动布局后,sizeToFit 方法执行它在 cellForRowAtIndexPath 方法中应该执行的操作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-29
      • 2013-06-25
      • 1970-01-01
      • 2012-02-06
      • 1970-01-01
      • 2015-07-02
      • 1970-01-01
      • 2012-01-06
      相关资源
      最近更新 更多