我试图在点击附件按钮时从表格行的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);
但我想了解我做错了什么。
答案 0 :(得分:0)
我从未得到textLabel的框架或边界,但这种组合最终得到了我所需要的。行右侧附近的位置(即在旋转后更新弹出位置的accessoryButton。rdelmar's SO post让我走了:
- (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;
}
}
}
}