【问题标题】:Table Cell not displaying correctly表格单元格显示不正确
【发布时间】:2013-12-13 13:10:45
【问题描述】:

我设计了一个自定义表格单元格,如下所示:

但是,它呈现如下:

如您所见,第二个标签位于下一个单元格中。知道如何解决此问题并使单元格按照我的设计显示吗?

【问题讨论】:

  • 你试过了吗? [标签 setAdjustsFontSizeToFitWidth:YES];
  • 并确保标签的边框处理得当。
  • 默认单元格中是否可以有2个字幕或2行显示字幕

标签: ios uitableview tablecell


【解决方案1】:

你需要实现UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

这个方法:

向代理询问指定行的高度 位置。

在此处为每行指定一个最小高度。此外,如果不同的行根据内容具有不同的高度,则在这种情况下您必须根据内容计算高度。像这样的:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *stringText=[array objectAtIndex: indexPath.row];
    CGSize size = [stringText sizeWithFont:[UIFont fontWithName: "yourFont" size: fontSize] constrainedToSize:maxCGSize];
    return labelSize.height + padding;
}

【讨论】: