I have a table view with a bunch of cells in it. I want to get the frame of a cell, not in its superview's coordinate space, but in the table view's coordinate space.
我有一个表格视图,里面有一堆单元格。我想得到一个单元格的框架,而不是在超视图的坐标空间中,而是在表格视图的坐标空间中。
A picture would probably explain this better
一张图片可能会更好地解释这一点
See the red rectangle? That is the frame of the table cell. And the blue rectangle is the frame of the table view. I want to get the red rectangle that is relative to the blue rectangle.
看到红色矩形?那是表格单元格的框架。蓝色矩形是表格视图的框架。我想获得相对于蓝色矩形的红色矩形。
I tried to use the frame
property of UITableViewCell
. But that doesn't work because it returns (0, 0, 320, 44). The Y value shouldn't be zero because obviously the cell is not at the top of the screen.
我试图使用UITableViewCell的frame属性。但这不起作用,因为它返回(0,0,320,44)。 Y值不应该为零,因为显然单元格不在屏幕的顶部。
According to the view hierarchy, this is the intended behaviour, because the superview of a table cell is apparently this UITableViewWrapperView
, not the table view:
根据视图层次结构,这是预期的行为,因为表格单元格的超级视图显然是这个UITableViewWrapperView,而不是表格视图:
while the table view is this:
而表视图是这样的:
So is there a method/property that can get me a CGRect
that represents the frame of the table view cell in the coordinate space of the table view?
那么有没有一个方法/属性可以让我得到一个CGRect来表示表视图坐标空间中表视图单元的框架?
I think there is a function that can convert a CGRect
in a coordinate space to another. But I forgot its name and how to use it. I don't know whether it is helpful in this situation.
我认为有一个函数可以将坐标空间中的CGRect转换为另一个。但我忘了它的名字以及如何使用它。我不知道这种情况是否有帮助。
1 个解决方案
#1
15
Please use below code
请使用以下代码
let rectOfCell= tableView.rectForRowAtIndexPath(indexPath)
let rectOfCellInSuperview = tableView.convertRect(rectOfCell, toView: tableView.superview)
#1
15
Please use below code
请使用以下代码
let rectOfCell= tableView.rectForRowAtIndexPath(indexPath)
let rectOfCellInSuperview = tableView.convertRect(rectOfCell, toView: tableView.superview)