【问题标题】:Cells Labels Not Resizing Until Table Scrolling单元格标签在表格滚动之前不会调整大小
【发布时间】:2013-04-03 11:18:19
【问题描述】:

我正在使用此代码来调整维护单元格标题的标签的大小。问题是单元格标签在我滚动表格之前不会调整大小。单元格需要消失才能正确调整其标签大小。我可以做些什么来调整所有单元格标签的大小而无需之前滚动?

感谢阅读。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    ExampleTableViewCell *cell = (ExampleTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"ExampleTableViewCell"];

    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ExampleTableViewCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }

    cell.title.text = [self.test objectAtIndex:[indexPath row]];

    //Calculates the expected size based on the font and linebreak mode of the label.
    CGSize maximumLabelSize = CGSizeMake(200, [UIScreen mainScreen].bounds.size.width);

    CGSize expectedLabelSize = [cell.title.text sizeWithFont:cell.title.font constrainedToSize:maximumLabelSize lineBreakMode:cell.title.lineBreakMode];

    // Adjusts the label the the new height.
    CGRect newFrame = cell.title.frame;
    newFrame.size.height = expectedLabelSize.height;
    cell.title.frame = newFrame;

    return cell;
}

【问题讨论】:

  • 贴出委托方法heightForRowAtIndexPath的代码..
  • 这里我只是返回单元格的最大大小

标签: ios objective-c uitableview uilabel


【解决方案1】:

尝试在

中编写相同的代码

willDisplayCell UItableViewController 方法

另一种方法是 .... 在您的 ExampleTableViewCell 类中实现

- (void)layoutSubviews{
}

在上述函数中设置标题的框架。它应该工作

【讨论】:

  • 它有效,但只是第一次。之前访问过此视图后,当我切换回此视图时,我仍然遇到同样的问题
【解决方案2】:

您必须实现方法 heightForRowAtIndexPath 并返回单元格的高度,然后您的问题就解决了 :-)

【讨论】:

    【解决方案3】:

    我发现 Matt Long 的解决方案非常有用 (http://www.cimgf.com/2009/09/23/uitableviewcell-dynamic-height/)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-08-22
      • 1970-01-01
      • 2019-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多