从 Table 单元格文本标签的右侧显示弹出窗口

Present Popover from Right Side of Table Cell Text Label

我试图在点击附件按钮时从 table 行的 textLabel 显示一个弹出窗口。理想情况下,它会从标签的最右边缘水平显示(可能因文本长度而异),垂直显示标签的中间高度。理论上,我认为以下是正确的,但它是从该行的左上角开始显示的。

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
    StackNameViewController *stackNameViewController = [[StackNameViewController alloc] init];
    stackNameViewController.navigationItem.title = NSLocalizedString (@"infoPopoverNavbarTitle", @"Info - Navbar title for Info popover.");

    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:stackNameViewController];
    UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:navController];
    [popover setDelegate:self];

    TableViewCell *cell = (TableViewCell *)[tableView cellForRowAtIndexPath:indexPath];

    CGRect labelFrame = cell.textLabel.bounds;
    NSLog(@"Text Label frame: %@", NSStringFromCGRect(labelFrame));
    CGRect popRectLocation = CGRectMake(labelFrame.origin.x, labelFrame.origin.y, labelFrame.size.width, labelFrame.size.height / 2.0);

    [popover presentPopoverFromRect:popRectLocation inView:cell.contentView permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}

我已经尝试从边界和框架中获取 textLabel 的框架。他们总是记录

Text Label frame: {{0, 0}, {0, 0}}

我可以通过硬编码在一定程度上伪造它

CGRect popRectLocation = CGRectMake(0, 0, 180, 40 / 2.0);

但我想了解我做错了什么。

我从来没有得到 textLabel 的框架或边界,但这种组合最终得到了我需要的东西。靠近行右侧的位置(即在旋转后更新弹出窗口位置的 accessoryButton。 让我继续:

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
   StackPropertiesTableViewController *stackPropertiesTableViewController = [[StackPropertiesTableViewController alloc] init];
   UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:stackPropertiesTableViewController];
   self.stackPropertiesPopoverController = [[UIPopoverController alloc] initWithContentViewController:navController];
   [self.stackPropertiesPopoverController setDelegate:self];
   TableViewCell *cell = (TableViewCell *)[tableView cellForRowAtIndexPath:indexPath];

    // Works for faking the display from Info accessoryView, but doesn't update it's location after rotate
    CGRect contentViewFrame = cell.contentView.frame;
    CGRect popRect = CGRectMake(contentViewFrame.origin.x + contentViewFrame.size.width, contentViewFrame.size.height/2.0, 1, 1);
    [self.stackPropertiesPopoverController presentPopoverFromRect:popRect inView:cell permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
    // Need this so popover knows which row it's on after rotate willRepositionPopoverToRect
    [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
}

旋转后需要更新弹出窗口相对于行宽的位置。不要忘记声明 UIPopoverControllerDelegate

- (void)popoverController:(UIPopoverController *)popoverController willRepositionPopoverToRect:(inout CGRect *)rect inView:(inout UIView *__autoreleasing *)view
{        
   if (self.stackPropertiesPopoverController == popoverController)
   {
      NSIndexPath *itemPath = self.tableView.indexPathForSelectedRow;
      if (itemPath)
         {
           TableViewCell *cell = (TableViewCell *)[self.tableView cellForRowAtIndexPath:itemPath];
           if (cell)
              {
                 CGRect contentViewFrame = cell.contentView.frame;
                 CGRect popRect = CGRectMake(contentViewFrame.origin.x + contentViewFrame.size.width, contentViewFrame.size.height/2.0, 1, 1);
                 *rect = popRect;
               }
          }
    }
}