本文介绍了如何设置自定义UIView的大小/位置以放置在自定义UITableViewCell中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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.

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

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?

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

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?

希望这是有道理的。

结构是:

动态自定义UITableCellView(子类)


  • 在heightForRowAtIndexPath中动态确定rowHeight

  • 在layoutSubviews中它包括循环子视图(我有自定义UIView)并在每个上调用layoutSubview

在自定义UIView(代表一行项目,例如UILabel)


  • 在layoutSubview中 - 问题:如何设置这个子视图的框架大小?

  • 我假设帧的x,y应该是0,0?

  • 对于宽度/高度,您如何获得应该设置的超视区的完整大小?

  • superview是否应该在layoutSubviews之前将此信息传递给它?

  • 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?

推荐答案

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

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];

这篇关于如何设置自定义UIView的大小/位置以放置在自定义UITableViewCell中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 07:21