UITableViewCell accessoryView框架宽度/高度为零

时间:2015-02-27 02:33:18

标签: ios objective-c uitableview cocoa-touch frame

我试图在点击配件时显示来自UIPopoverController accessoryView的UITableViewCell。我用:

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

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


我正在使用:

  • iOS 8
  • 故事板
  • 预定义的详细信息附件(UITableViewCellAccessoryDetailButton
  • iPad

如果您需要更多代码可以回答,请告诉我,我会尽力为您解答。提前谢谢!

3 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:0)

accessoryView坐标系内UITableViewCell的框架。您需要使用UIView上的方法将其转换为TableView坐标系:-convertRect:fromView:

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

答案 2 :(得分:0)

我从未得到过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;
               }
          }
    }
}