Getting the exact location of a UITableViewCell

后端 未结 9 587
滥情空心
滥情空心 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: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;
    

提交回复
热议问题