【问题标题】:Draw rectangle in custom table cell在自定义表格单元格中绘制矩形
【发布时间】:2023-03-25 11:24:02
【问题描述】:

如何在自定义表格单元格类中绘制矩形?该单元格当前有一个带有一些文本标签的背景图像。我想在每个标签后面画一个矩形,以便更容易阅读详细的背景图像。

我知道我可以只设置标签的背景颜色,但我想在背景颜色和文本之间设置填充。如果可能的话,我很想知道怎么做! :)

我在 Three20 中对 TTTableMessageItemCell 进行子类化,调用下面的方法,您可以在其中使用单元格的子视图,

- (void)layoutSubviews {

[super layoutSubviews];

CGFloat padding = 16;
CGFloat boxWidth = self.contentView.width - 2*padding;
CGFloat textWidth = boxWidth - (padding*2);
CGFloat textHeight = 100;
CGFloat top = kTableCellSmallMargin;

// Position Heading Text
_titleLabel.frame = CGRectMake(padding, top, textWidth, _titleLabel.font.ttLineHeight);
top += _titleLabel.height;

// Position Detail Text
[self.detailTextLabel sizeToFit];
self.detailTextLabel.top = top+2*padding;
self.detailTextLabel.left = 2*padding;
self.detailTextLabel.width = textWidth;
self.detailTextLabel.height = 100;    
}

我希望将矩形放在 _titleLable 和 detailTextLabel 标签后面。

编辑 我已经能够使用以下方法添加正确的框,

UIView *view = [[UIView alloc] init];
view.backgroundColor = [UIColor whiteColor];
view.frame = CGRectMake(padding, top, textWidth, textHeight+2*padding);
[self insertSubview:view belowSubview:self.detailTextLabel];

它位于标签之上,我似乎无法将它放在它后面......

编辑 我将视图添加到错误的子视图中,修复了它,

[[self.subviews objectAtIndex:0] insertSubview:view atIndex:0];

【问题讨论】:

标签: iphone objective-c cocoa-touch tablecell


【解决方案1】:

您可以将标签添加到视图,并将这些标签添加到单元格。

您可以使用insertSubview:belowSubview: 在标签后面添加视图。使用 backgroundColor 和正确的框架,他们会做你想做的事。

你也可以带上detailLabel to front

【讨论】:

  • 我正在覆盖现有的单元格类,所以我不知道是否可以更改添加标签的位置...
  • 您可以从其父视图中删除标签并将其添加到 uiview。顺便说一句:向我们展示您的代码会更容易为您提供帮助
  • 我没有在我最初的帖子中包含代码,因为我认为它没有帮助。我现在将其添加,以便您查看我正在使用的内容
  • 我已经设法添加了框,但我无法将它放在标签后面,我在编辑中添加了我的新代码
  • 使用 insertSubview:view atIndex 修复它
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-11-11
  • 2015-07-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-04
相关资源
最近更新 更多