2

我想获取 UITableVIewCell 相对于屏幕的位置,而不是相对于 tableview 的位置。所以,如果我滚动表格视图,位置就会更新。

4

3 回答 3

6

您应该使用两个步骤来实现这一点:

  1. 使用UITableView的以下方法之一获取选定单元格/页眉或页脚的CGRect信息:
- (CGRect)rectForHeaderInSection:(NSInteger)section;
- (CGRect)rectForFooterInSection:(NSInteger)section;
- (CGRect)rectForRowAtIndexPath:(NSIndexPath *)indexPath;

2.使用UITableViewconvertRect方法将CGRect转换为表格的容器视图。例如。要获取第一个标头的当前位置,您可以使用以下代码:

    CGRect rect = [mytable convertRect:[mytable rectForHeaderInSection:0] toView:[mytable superview]];

如果你想要这些动态,你可能想在 UITableView 的委托方法中包含这个代码:

  - (void)scrollViewDidScroll:(UIScrollView *)scrollView_ {.....

我使用类似技术所做的一个示例是在 github - 如何修复表头行:

https://github.com/codedad/SO_Fixed_TableHeader_iOS

希望对你有帮助!

于 2013-09-16T15:04:57.770 回答
0

您需要从孩子到父母的位置:

float x = cell.frame.origin.x;
float y = cell.frame.origin.y;

UIView* parent = cell.parent;

while(parent != nil)
{
    x += parent.frame.origin.x;
    y += parent.frame.origin.y;
    parent = parent.parent;
}
于 2013-09-16T14:06:50.947 回答
0

使用-[UITableView rectForRowAtIndexPath:]方法。请记住,如果您在单元格不在屏幕上时尝试获取此信息,您将得到意想不到的结果。然后,您可以使用该方法将其转换为您的视图控制器的视图(我认为这就是您所说的屏幕)-[UIView convertRect:toView:]

参考:https ://developer.apple.com/library/ios/documentation/uikit/reference/UITableView_Class/Reference/Reference.html#jumpTo_49

https://developer.apple.com/library/ios/documentation/uikit/reference/uiview_class/UIView/UIView.html#jumpTo_65

于 2013-09-16T14:39:36.890 回答