获取UITableViewCell的确切位置
给定一个UITableView ,我怎么find一个特定的UITableViewCell的位置? 换句话说,我想得到它的框架相对于我的iPhone屏幕,而不是相对于UITableView 。 所以,如果我的UITableView滚动,每个UITableViewCell的位置应该在屏幕上更高等。 
 您也可以使用rectForRowAtIndexPath方法通过发送indexPath来获取UITableView的位置。 
 - (CGRect)rectForRowAtIndexPath:(NSIndexPath *)indexPath 
所以使用如下:
 CGRect myRect = [tableView rectForRowAtIndexPath:indexPath]; 
除了rectForRowAtIndexPath你需要考虑滚动。
试试这个代码:
  // 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); 
尝试以下操作(发送nil作为toView参数意味着要将其转换为窗口坐标):
 CGRect r = [cell convertRect:cell.frame toView:nil]; 
请记住,如果特定的行目前不可见,那么可能不会有UITableViewCell – 因此,在使用该代码之前,您可能需要检查单元格是否有效(例如,不为零)
尝试它
  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); 
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; 
Swift 3
 相对于tableView : 
 let rect = self.tableView.rectForRow(at: indexPath) 
 相对于Screen : 
 如果你只知道cell , 
 if let indexPath = tableView.indexPath(for: cell) { let rect = self.tableView.rectForRow(at: indexPath) let rectInScreen = self.tableView.convert(rect, to: tableView.superview) } 
 如果您知道indexPath则不需要调用if语句。 
 对于未来的观众,我在UITableView获取单元格的可靠框架时遇到了困难。 我试图在iPad上显示一个UIAlertController样式的UIAlertController ,需要popup窗口。 最后,这种方法取得了最好的结果: 
 // 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; 
如果你真的需要专门转换为窗口中的一个点,你可以这样做:
 [yourAppDelegate.window convertPoint:[cell.contentView.center] fromView:[cell.contentView]]; 
我用细胞中心坐标,但你可以使用任何你想要的点。
弗拉基米尔是正确的,小心行不可见(或已被回收)。
-S
Tomasz和Jhaliya的答案,以防万一任何人(其他人)与此斗争:
 var cellRect = tableView.rectForRow(at: indexPath) cellRect = cellRect.offsetBy(dx: -tableView.contentOffset.x, dy: -tableView.contentOffset.y) 
- IOHIDEventQueue.c中的DP5错误
- UICollectionViewselect并取消select问题
- iTunes Connect提交代码签名授权错误Xcode 8
- 必须在App Store发布时增加哪些iOS应用程序版本/内部版本号?
- 按下UINavigationController的后退button时执行动作
- 警告:顶点着色器'v_gradient'的输出没有被片段着色器读取
- 在AppDelegate.m的屏幕上获取当前显示的UIViewController
- ? 在Xcode 6 beta 6中没有名为“下标”的成员
- 有关从iPhone应用程序和S3上传照片的build筑和devise问题