ios 获取 UITableViewCell 的确切位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6280288/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 20:15:33  来源:igfitidea点击:

Getting the exact location of a UITableViewCell

iphoneiosuitableview

提问by CodeGuy

Given a UITableView, how can I find the location of a specific UITableViewCell? In other words, I want to get its frame relative to my iPhone screen, not relative to the UITableView. So if my UITableViewis scrolled up, the location of each UITableViewCellshould be higher on the screen, etc.

给定 a UITableView,我怎样才能找到特定的位置UITableViewCell?换句话说,我想让它的框架相对于我的 iPhone 屏幕,而不是相对于UITableView. 所以如果我UITableView向上滚动,每个位置UITableViewCell在屏幕上应该更高,等等。

回答by Jhaliya

You could also use the rectForRowAtIndexPathmethod to get the location of a UITableViewby sending the indexPathfor that.

您还可以使用该rectForRowAtIndexPath方法UITableView通过发送indexPath来获取 a 的位置。

- (CGRect)rectForRowAtIndexPath:(NSIndexPath *)indexPath

So use as below:

所以使用如下:

CGRect myRect = [tableView rectForRowAtIndexPath:indexPath];

回答by Tomasz

Apart from rectForRowAtIndexPath you need to consider the scrolling.

除了 rectForRowAtIndexPath 您还需要考虑滚动。

Try this code:

试试这个代码:

 // Get the cell rect and adjust it to consider scroll offset
 CGRect cellRect = [tableView rectForRowAtIndexPath:indexPath];
 cellRect = CGRectOffset(cellRect, -tableView.contentOffset.x, -tableView.contentOffset.y);

回答by Vladimir

Try the following(sending nil as a toView parameter means you want to convert you rect to window coordinates):

尝试以下操作(将 nil 作为 toView 参数发送意味着您要将 rect 转换为窗口坐标):

CGRect r = [cell convertRect:cell.frame toView:nil];

And remember that if particular row is not currently visible then there may not be UITableViewCell for it - so before using that code you may need to check if cell is valid (not nil for example)

请记住,如果特定行当前不可见,则可能没有 UITableViewCell 它 - 所以在使用该代码之前,您可能需要检查单元格是否有效(例如,不是 nil)

回答by William Hu

Swift 3

斯威夫特 3

Relative to the tableView:

相对于tableView

let rect = self.tableView.rectForRow(at: indexPath)

Relative to the Screen:

相对于Screen

If you only know the cell,

如果你只知道cell,

if let indexPath = tableView.indexPath(for: cell) {
     let rect = self.tableView.rectForRow(at: indexPath)
     let rectInScreen = self.tableView.convert(rect, to: tableView.superview)
}

If you know the indexPaththen don't need call the ifstatement.

如果您知道,indexPath则不需要调用该if语句。

回答by Milan Vadgama

try it in

试一试

didSelectRowAtIndexPathmethod

didSelectRowAtIndexPath方法

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];


        // get current location of selected cell

        CGRect rectInTableView = [tableView rectForRowAtIndexPath:indexPath];

        CGRect rectInSuperview = [tableView convertRect:rectInTableView toView:[tableView superview]];


         NSLog(@"Cell Y Is %f",rectInSuperview.origin.y);

        NSLog(@"Cell X Is %f",rectInSuperview.origin.x);

回答by noRema

Jhaliya's answer wasn't quite enough for me, I needed to do some more manipulations to get it working. My tableView was added to a viewController and its location on the right half way down the screen. So you need to take the tableView origin into account aswel as the scroll offset.

Jhaliya 的回答对我来说还不够,我需要做更多的操作才能让它工作。我的 tableView 被添加到一个 viewController 中,它的位置在屏幕下方的右侧。因此,您需要考虑 tableView 原点以及滚动偏移量。

CGRect rowRect = [tableView rectForRowAtIndexPath:indexPath];
CGPoint offsetPoint = [self.infoTableView contentOffset];

// remove the offset from the rowRect
rowRect.origin.y -= offsetPoint.y;

// Move to the actual position of the tableView
rowRect.origin.x += self.infoTableView.frame.origin.x;
rowRect.origin.y += self.infoTableView.frame.origin.y;

回答by Rob Sanders

For future viewers, I was having trouble getting a reliable frame for cells in a UITableView. I was trying to display a UIAlertControllerin ActionSheet style on an iPad which needs a popover presentation. In the end this approach yielded the best results:

对于未来的观众,我在为UITableView. 我试图UIAlertController在需要弹出式演示文稿的 iPad 上显示ActionSheet 样式。最后,这种方法产生了最好的结果:

// 44 is the standard height for a cell in a UITableView
// path is the index path of the relevant row
// controller is the UIAlertController
CGRect frame = CGRectZero;
frame.origin.y = 44 * path.row;
frame.origin.x = table.frame.origin.x;
frame.size = CGSizeMake(table.frame.size.width, 44);
controller.popoverPresentationController.sourceRect = [tableView convertRect:frame toView:self.view];
controller.popoverPresentationController.sourceView = self.view;

回答by T. Benjamin Larsen

Swift-version of Tomasz and Jhaliya's answers in case anyone (else) struggles with this:

Tomasz 和 Jhaliya 的 Swift 版本的答案,以防有人(其他人)遇到这个问题:

var cellRect = tableView.rectForRow(at: indexPath)
cellRect = cellRect.offsetBy(dx: -tableView.contentOffset.x, dy: -tableView.contentOffset.y)

回答by Steve N

If you really need to convert specifically to a point in the window, you could do this:

如果您确实需要专门转换为窗口中的一个点,您可以这样做:

[yourAppDelegate.window convertPoint:[cell.contentView.center] fromView:[cell.contentView]];

I used the cells center coordinate, but you could use any point you want.

我使用了单元格中心坐标,但您可以使用任何您想要的点。

Vladimir is right, watch out for rows that are not visible (or that have been recycled).

Vladimir 是对的,注意不可见(或已回收)的行。

-S

-S