【问题标题】:UItableviewcontroller SizeToFit not working on UILabel fieldUItableviewcontroller SizeToFit 不适用于 UILabel 字段
【发布时间】:2015-12-01 14:29:39
【问题描述】:

您好,我在我的 UILabel 字段上使用 sizetofit,当我加载 tableview 控制器时它工作正常,但是当我上下滚动时开始切割字母并换行到另一行。这是我在 CellForRowAtIndexPath 中使用的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"subjectCell" forIndexPath:indexPath];

    // Configure the cell...

    // Load and display subject photo image
    UIImageView *subjectImageView = (UIImageView *)[cell viewWithTag:200];
    subjectImageView.image = [UIImage imageNamed:subjectImages[indexPath.row]];


    // Load and display subject label
    UILabel *subjectLabel = (UILabel *)[cell viewWithTag:201];
    subjectLabel.text = [subjectItems objectAtIndex:indexPath.row];
    [subjectLabel sizeToFit];// call this to fit size of the label according to text



    return cell;
}

这是我最初加载的 tableviewcontroller 的屏幕截图:

这里上下滚动后可以看到描述被剪掉了

感谢您为解决此问题提供的任何帮助。

干杯

卡森

【问题讨论】:

  • 显示您的cellForRowAtIndexPath: 方法。
  • @rmaddy 见上文我现在显示完整的 cellForRowAtIndexPath
  • 在sizeToFit之后尝试setNeedDisplay

标签: ios objective-c uitableview sizetofit


【解决方案1】:

将sizeToFit 用于UILabel 可能会在UITableView 中导致类似这样的混乱,并重复使用单元格。我能想到的解决方案之一是在调用 sizeToFit 之前为标签宽度设置框架的宽度,每次为标签分配文本时都必须这样做。它应该看起来像这样:

CGRect frame = subjectLabel.frame;
frame.size.width = 100.0; //try to adjust this value 
subjectLabel.frame = frame;
[subjectLabel sizeToFit];

【讨论】:

  • 非常感谢,已经解决了:-)
猜你喜欢
  • 2013-04-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多