【问题标题】:UITableView with UILabel SizeToFit get mess when Scrolling滚动时带有 UILabel SizeToFit 的 UITableView 会变得一团糟
【发布时间】:2013-06-01 17:20:45
【问题描述】:

大家好,我的tableview 有问题,我用uilabelSizeToFit 制作单元格,然后计算UILabel 高度以设置单元格高度一切正常,除非我滚动我的tableView文本变得像每行一个字符一样奇怪:

我的TableViewVell方法是:

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

if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}


UILabel *label = (UILabel *)[cell viewWithTag:1];
label.text = [someArray objectAtIndex:indexPath.row];
// Set the UILabel to fit my text;
messageLabel.lineBreakMode = UILineBreakModeWordWrap;
messageLabel.numberOfLines = 0;
[messageLabel sizeToFit];


return cell;   
}

单元格高度保持在正确的大小仅与UILabel有关... 这是我加载视图时的结果:

First screenshot

这是在我开始滚动 TableView 之后:

Second screenshot

任何帮助???

【问题讨论】:

  • 什么是messageLabel,它与上面一行的标签有什么不同
  • 你在哪里创建了单元格的视图。您可能只想在那里设置标签的 lineBreakMode 属性。
  • awooo 抱歉我修好了,我只是更改了名称以使其易于阅读
  • 我正在使用 Storyboard,所以我在那里创建它并使用 [cell ViewWithTag:1]
  • @Lukas Spieß 你到底编辑了什么?

标签: ios objective-c uitableview uilabel sizetofit


【解决方案1】:

您可以设置标签的首选宽度属性。这对我有用

preferred Width

【讨论】:

    【解决方案2】:

    您可能应该将您的单元格子类化并清理上的 UILabel

     override func prepareForReuse() {
        super.prepareForReuse()
        self.label.text = nil
    }
    

    【讨论】:

      【解决方案3】:

      如果你没有使用自动布局,这也可以通过在UIView上添加一个类别来解决

      public func sizeToFit(constrainedSize: CGSize) -> CGSize {
        var newSize = sizeThatFits(constrainedSize)
        newSize.width = min(newSize.width, constrainedSize.width)
        newSize.height = min(newSize.height, constrainedSize.height)
        self.size = newSize
        return newSize
      }
      

      这个想法是使用 sizeThatFits 和一个受限制的大小来确定要采用的大小,然后使用最小的预期宽度/高度。

      然后你只需要做类似的事情

      yourLabel.sizeToFit(self.contentView.size) // or
      yourLabel.sizeToFit(CGSize(width: maxWidth, height: self.contentView.size.height))
      

      【讨论】:

        【解决方案4】:

        sizeToFit 方法尊重框架的宽度并调整height。并且表格视图单元格重用单元格。结合这两种行为可能会导致您描述的问题。

        您的一个标签的内容非常短(例如,在第 4 个单元格中带有单词“jio”),使用 sizeToFit 调整大小,结果宽度小于您想要的原始宽度。

        之后,表格视图重用单元格,sizeToFit 仍然尊重从先前调用 sizeToFit 计算的小宽度。

        解决方案:

        1) 每次调用sizeToFit 之前,将框架的宽度设置为您原来的预期标签宽度:

        CGRect frame = messageLabel.frame;
        frame.size.width = 100.0; //you need to adjust this value 
        messageLabel.frame = frame;
        

        2) 使用您的行高计算并设置框架高度。无需使用sizeToFit

        【讨论】:

        • 出现此错误:使用不兼容类型“CGRect”(又名“struct CGRect”)的表达式初始化“CGRect *”(又名“struct CGRect *”)在 CGRect *frame = messageLabel 行上。框架;
        • 好的,它工作得很好非常感谢你,但毕竟我需要 SizeToFit,因为如果我不使用它,文本只会填充一行。无论如何,这段代码可以正常工作: messageLabel.lineBreakMode = UILineBreakModeWordWrap; messageLabel.numberOfLines = 0; CGRect frame = messageLabel.frame;框架.大小.宽度 = 236.0; //你需要调整这个值 messageLabel.frame = frame; [messageLabel sizeToFit];
        • @DekelMaman 是的。对于第一个解决方案。我提到你需要在每次调用 sizeToFit 之前调整宽度。第二种解决方案不需要 sizeToFit。
        • 检查 XIB 或 StoryBoard - 检查自动布局?
        【解决方案5】:

        【讨论】:

          猜你喜欢
          • 2016-11-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-09-17
          • 2013-07-21
          • 1970-01-01
          • 2014-05-25
          • 2011-06-18
          相关资源
          最近更新 更多