【问题标题】:Content of custom UITableViewCell dynamic height in iOS8 has different widthsiOS8中自定义UITableViewCell动态高度的内容有不同的宽度
【发布时间】:2015-04-12 07:54:13
【问题描述】:

我在将 xcode 升级到 6.3 后遇到了这个问题。使用 xcode 6.2 编译的前一个版本没有这个问题并且效果很好。

问题是:切换到其他选项卡并返回后,或者从push segue返回后,大多数单元格的宽度会缩小一点。但有些没有。它看起来像关注

第 2 行单元格中的标签内容具有正确的宽度。并且所有单元格在首次启动后都以正确的宽度显示内容。 在上图中,有两个 uitableviewcells。 内容视图:蓝色; 背景视图:浅绿色; 内容标签:紫色;
第一个单元格中的 bg 视图和 contentLabel 缩小了。那是我不想要的。

我使用故事板来设置约束。 bg 视图的约束。 labelview 的约束 我还尝试根据这个问题UITableView layout messing up on push segue and return. (iOS 8, Xcode beta 5, Swift) 缓存单元格高度,但它不起作用。

//In viewDidLoad
self.tableView.estimatedRowHeight = 100.0;
self.tableView.rowHeight = UITableViewAutomaticDimension;
self.tableView.contentInset = UIEdgeInsetsZero;

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
    float height = [[self.estimatedRowHeightCache objectForKey:[NSString stringWithFormat:@"%ld", indexPath.row]] floatValue];
    if (height) {
        return height;
    } else {
        return UITableViewAutomaticDimension;
    }

}
//cache row height. I observed this doesn't been called in first launching in debugging.
- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.estimatedRowHeightCache setValue:[NSNumber numberWithFloat:cell.frame.size.height] forKey:[NSString stringWithFormat:@"%ld", indexPath.row]];
}

感谢您的帮助!

【问题讨论】:

  • 可以为单元格的 contentView 设置背景颜色吗?或设置单元格的背景颜色,或者您已经设置并检查?
  • 我已经为每个视图设置了背景颜色。内容视图:蓝色;背景视图:浅绿色,是添加到 contentView 作为标签背景的视图。 contentLabel:紫色。
  • 你能分享你设置这些颜色的代码吗?还有,你用的是xib吗?
  • 我正在使用storyboard,并且已经上传了约束的img。我使用自动布局。

标签: ios objective-c xcode uitableview


【解决方案1】:

好吧,您没有为contentView 顶部的视图设置水平空间约束。您只是为您的标签及其父视图设置水平空间约束。

这样做

  1. 选择包含标签的视图
  2. Editor > Pin > 引导空间到 SuperView

【讨论】:

    【解决方案2】:

    为Tableview WillDisplayCell实现DataSource方法

    或者您可以在单元格类(如果是自定义单元格)中实现方法 - (void)awakeFromNib 并设置单元格框架。

    【讨论】:

      猜你喜欢
      • 2016-01-25
      • 1970-01-01
      • 2016-09-03
      • 1970-01-01
      • 1970-01-01
      • 2019-10-12
      • 2011-03-22
      • 2015-08-06
      • 1970-01-01
      相关资源
      最近更新 更多