如何设置自定义 UIView 的大小/位置以将其放置在自定义 UITableViewCell 中?

发布于 2024-10-25 10:21:24 字数 933 浏览 1 评论 0原文

如何设置以编程方式放置在 UITableViewCell 内的子视图的大小?

我有一个自定义(子类)UITableViewCell,其中单元格的一部分包含许多行(动态),这些行被实现为自定义 UIView。这个想法是创建自定义视图(这是一行项目),然后当 UITableViewCell 命中layoutSubviews时,它将(a)设置自己重新定位,(b)循环并在每一行上调用“layoutSubviews”用于行的自定义 UIView。

问题 - 我不确定如何正确设置自定义 UIView 行的大小/位置?如何在自定义 UIView layoutSubviews 方法中确定其 x、y、宽度、高度以便定位它?

例如,我实际上是否需要手动在 UITableCellView 的layoutSubviews 方法中循环遍历所有行并调用它们的自定义方法来向它们传递它们需要的位置/宽度/高度?然后,当自定义 UIView 行的layoutSubview 方法被击中时,它会知道查找存储这些值的实例变量以使用它们?

希望这是有道理的。

结构是:

动态自定义UITableCellView(子类化)

  • 在layoutSubviews中的heightForRowAtIndexPath中动态确定rowHeight,
  • 它包括循环遍历子视图(我拥有的自定义UIView)并在每个

自定义UIView中调用layoutSubview(其中代表layoutSubview中的一行项目,例如UILabel)

  • - 问题:如何设置此子视图的框架大小?
  • 我假设框架的 x,y 应该是 0,0?
  • 对于宽度/高度,如何获得应设置的超级视图区域的完整尺寸?
  • 超级视图是否应该在布局子视图之前将此信息传递给它?

How do I set the size of a subview which is being placed programmatically inside a UITableViewCell?

I have a custom (subclassed) UITableViewCell in which part of the cell includes a number of rows (dynamic) which are implemented as a custom UIView. The idea is to create the custom view (which is a row of items) and then when the UITableViewCell hits layoutSubviews it will (a) set itself up re positioning and (b) loop through and call "layoutSubviews" on each of the rows of the custom UIViews it is using for the rows.

Question - I'm not sure how to correctly set the size/position of the custom UIView row? How do I, within the custom UIView layoutSubviews method, determine its x,y,width,height so I can position it?

Do I actually for example need to manually, within the UITableCellView's layoutSubviews method, loop through all the rows and call a custom method of them to pass them their position/width/height's that they will need to be at? Then when the custom UIView row's layoutSubview method is hit it would know to look up it's instance variable which stored these values to use them?

Hopefully this makes sense.

Structure is:

Dynamic Custom UITableCellView (subclassed)

  • determines rowHeight dynamically in heightForRowAtIndexPath
  • in layoutSubviews it includes looping through subviews (of custom UIView I have) and calls layoutSubview on each of these

In custom UIView (which represents a row of items, e.g. UILabel)

  • in layoutSubview - QUESTION: How do set the size of the frame of this subview?
  • I assume the x,y for the frame should be 0,0?
  • For the width/height how do you get the full size of the superview area to which it should set in?
  • Should the superview have passed this information down to it prior to the layoutSubviews?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

み零 2024-11-01 10:21:24

您可以使用调整大小方法向 UIView 添加类别。

UIView+ResizeMethods.h

- (void) setHeight: (CGFloat) height;
- (void) setWidth: (CGFloat) width;
- (void) setTop: (CGFloat) top;
- (void) setLeft: (CGFloat) left;

UIView+ResizeMethods.m

- (void) setHeight: (CGFloat) height {
    CGRect frame = self.frame;
    frame.size.height = height;
    self.frame = frame;
}
- (void) setWidth: (CGFloat) width {
    CGRect frame = self.frame;
    frame.size.width = width;
    self.frame = frame;
}
- (void) setTop: (CGFloat) top {
    CGRect frame = self.frame;
    frame.origin.y = top;
    self.frame = frame;
}
- (void) setLeft: (CGFloat) left {
    CGRect frame = self.frame;
    frame.origin.x = left;
    self.frame = frame;
}

You can add a category to UIView with resize methods.

UIView+ResizeMethods.h

- (void) setHeight: (CGFloat) height;
- (void) setWidth: (CGFloat) width;
- (void) setTop: (CGFloat) top;
- (void) setLeft: (CGFloat) left;

UIView+ResizeMethods.m

- (void) setHeight: (CGFloat) height {
    CGRect frame = self.frame;
    frame.size.height = height;
    self.frame = frame;
}
- (void) setWidth: (CGFloat) width {
    CGRect frame = self.frame;
    frame.size.width = width;
    self.frame = frame;
}
- (void) setTop: (CGFloat) top {
    CGRect frame = self.frame;
    frame.origin.y = top;
    self.frame = frame;
}
- (void) setLeft: (CGFloat) left {
    CGRect frame = self.frame;
    frame.origin.x = left;
    self.frame = frame;
}
街角迷惘 2024-11-01 10:21:24

作为 tableViewCell 子类的一部分添加的任何视图都相对于单元格框架定位,即子视图原点的 x:0,y:0 将位于 tableCell 的左上角。
像这样的东西应该足以让你开始。

CGRect frame =[self frame];
frame.size.height=20.0f;
frame.origin.x=(self.frame.size.height/2)-frame.size.height;
frame.origin.y=0.0f;
[subview setFrame:frame];

any views added as part of the tableViewCell subclass are positioned relative to the cells frame, ie x:0,y:0 for the subview origin, would be the top left corner of the tableCell.
Something like this should be enough to get you started.

CGRect frame =[self frame];
frame.size.height=20.0f;
frame.origin.x=(self.frame.size.height/2)-frame.size.height;
frame.origin.y=0.0f;
[subview setFrame:frame];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文