Getting the exact location of a UITableViewCell

后端 未结 9 584
滥情空心
滥情空心 2021-01-30 20:10

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, n

相关标签:
9条回答
  • 2021-01-30 20:42

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

    var cellRect = tableView.rectForRow(at: indexPath)
    cellRect = cellRect.offsetBy(dx: -tableView.contentOffset.x, dy: -tableView.contentOffset.y)
    
    0 讨论(0)
  • 2021-01-30 20:49

    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).

    -S

    0 讨论(0)
  • 2021-01-30 20:50

    try it in

    didSelectRowAtIndexPath method

    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);
    
    0 讨论(0)
  • 2021-01-30 20:50

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

    // 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;
    
    0 讨论(0)
  • 2021-01-30 20:54

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

    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)

    0 讨论(0)
  • 2021-01-30 20:58

    You could also use the rectForRowAtIndexPath method to get the location of a UITableView by sending the indexPath for that.

    - (CGRect)rectForRowAtIndexPath:(NSIndexPath *)indexPath
    

    So use as below:

    CGRect myRect = [tableView rectForRowAtIndexPath:indexPath];
    
    0 讨论(0)
提交回复
热议问题