UITableViewCell accessoryView frame width/height 零

UITableViewCell accessoryView frame width/height zero

我试图在点击附件时从 UITableViewCell accessoryView 显示 UIPopoverController。我使用:

[self.popover presentPopoverFromRect:[[tableView cellForRowAtIndexPath:indexPath] accessoryView].frame inView:tableView permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

但是问题:...accessoryView].frame{{0, 0}, {0, 0}},所以弹出窗口显示在屏幕的左上角。为什么会这样? 如何获取 accessoryView 的实际框架?


我正在使用:

如果您需要更多代码来回答,请告诉我,我会尽力为您获取。提前致谢!

accessoryViewUITableViewCell坐标系中的坐标系。您需要使用UIView 上的方法将其转换为TableView 坐标系:-convertRect:fromView:.

致电[tableView convertRect:accessoryView.frame fromView:cell] // Code not tested

我从来没有得到 accessoryView 的框架或边界,但这种组合最终让我能够伪造它并得到我需要的东西。行右侧附近的位置(即在旋转后更新弹出位置的 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;
               }
          }
    }
}

这是因为 cell.accessoryView 为空。 cell.accessoryView 仅 returns 自定义 accessoryView.