【问题标题】:UITableViewCell imageView causes too much indentionUITableViewCell imageView 导致缩进过多
【发布时间】:2012-06-05 17:32:23
【问题描述】:

我有一个子类UITableViewCell,我正在为其设置imageView。我想让它保持在 60x60,所以我在我的子类的 layoutSubviews 方法中设置了 framebounds

- (void)layoutSubviews {
    [super layoutSubviews];

    self.imageView.bounds = CGRectMake(10,10,60,60);
    self.imageView.frame = CGRectMake(10,10,60,60);
    self.imageView.clipsToBounds = YES;

}//end

这很好用,直到我的图像超出了imageView 的范围:

因此,我确保将 clipsToBounds 设置为 YES:

但正如您所见,它仍然缩进 textLabeldetailTextLabel,尽管我认为不应该这样做。

我该如何解决这个问题?

【问题讨论】:

  • 如果 textLabel 和 detailTextLabel 右对齐,您是否尝试过设置文本对齐方式?
  • 这样设置边界和框架是多余的。设置框架隐式设置边界。并不是说这在这里有什么不同。
  • 如果您要进行子类化,我建议不要使用内置子视图(imageViewtextLabeldetailTextLabel),因为基类会以不受欢迎的方式为您重新定位它们。只需创建并添加您自己的图像视图和标签(在initawakeFromNib 中)。
  • @warrenm 是的,我想我应该创建自己的imageView,但我只是想看看我是否错过了一些简单的方法。

标签: ios cocoa-touch uitableview uiimageview


【解决方案1】:

您可以在-layoutSubviews 实现中设置textLabel 的位置:

- (void)layoutSubviews {
    [super layoutSubviews];

    self.imageView.bounds = CGRectMake(10,10,60,60);
    self.imageView.frame = CGRectMake(10,10,60,60);
    self.imageView.clipsToBounds = YES;

    CGFloat x = 10.0 + 60.0 + 10.0; // Padding, image width, and padding.

    CGRect frame = self.textLabel.frame;
    frame.origin.x = x;
    self.textLabel.frame = frame;

    frame = self.detailTextLabel.frame;
    frame.origin.x = x;
    self.detailTextLabel.frame = frame;

} //end

您可能想要调整x 的值。

希望这会有所帮助。

【讨论】:

    【解决方案2】:

    我认为我采用的更好的解决方案是不使用 imageView 中的构建,而是自行构建。这样效果更好。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-02
      • 2022-09-24
      • 1970-01-01
      • 1970-01-01
      • 2011-09-06
      相关资源
      最近更新 更多