如何从 UITableViewCell.accessoryView 调用 PopOver 控制器?

Posted

技术标签:

【中文标题】如何从 UITableViewCell.accessoryView 调用 PopOver 控制器?【英文标题】:How to call PopOver Controller from UITableViewCell.accessoryView? 【发布时间】:2010-05-12 22:53:45 【问题描述】:

首先我想说的是,我对 ipad/ipod/iphone 开发以及objective-c 真的很陌生。

话虽如此,我正在尝试使用 Xcode 和 IB 开发一个针对 iPad 的小型应用程序,基本上,我有一个表格,对于表格中的每个 UITableViewCell,我向附件视图添加了一个包含一个按钮图片。

代码如下:

UIImage *img = [UIImage imageNamed:@"myimage.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
CGRect frame = CGRectMake(0.0, 0.0, img.size.width, img.size.height);
button.frame = frame;   // match the button's size with the image size

[button setBackgroundImage:img forState:UIControlStateNormal];

// set the button's target to this table view controller so we can interpret touch events and map that to a NSIndexSet
[button addTarget:self action:@selector(checkButtonTapped:event:) forControlEvents:UIControlEventTouchUpInside];
button.backgroundColor = [UIColor clearColor];
cell.accessoryView = button;

到目前为止,一切都很好,现在的问题是我希望在用户点击单元格的附件视图上的按钮时出现 PopOver 控件。

我在tableView的“accessoryButtonTappedForRowWithIndexPath”上试过这个:

UITableViewCell *cell = [myTable cellForRowAtIndexPath:indexPath];
UIButton *button = (UIButton *)cell.accessoryView;

//customViewController is the controller of the view that I want to be displayed by the PopOver controller
customViewController = [[CustomViewController alloc]init];

popOverController = [[UIPopoverController alloc]
initWithContentViewController: customViewController];

popOverController.popoverContentSize = CGSizeMake(147, 122);

CGRect rect = button.frame;

[popOverController presentPopoverFromRect:rect inView:cell.accessoryView
permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];

这段代码的问题在于它在应用程序视图的顶部显示了弹出框,而在调试时我看到了“rect”的值,它们是:

x = 267
y = 13

所以我认为很明显为什么 PopOver 在视图上显示得如此之高,所以我的问题是,我怎样才能让 PopOver 的正确值出现在单元格的附件视图上的按钮下方?

另外,如您所见,我告诉它为“inView:”属性使用“cell.accessoryView”,可以吗?

【问题讨论】:

【参考方案1】:

尝试使用button.bounds 而不是button.frame,因为矩形是相对于inView

另外请注意,如果单元格位于屏幕底部附近,则弹出框可能不会以正确的大小显示,因为您强制箭头朝上。您应该手动处理或只使用 Any。

【讨论】:

【参考方案2】:
[popOverController presentPopoverFromRect:rect inView:cell.accessoryView
permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];

将 inView:cell.accessoryView 更改为 inView:cell

【讨论】:

【参考方案3】:

这里是快速的:

let cell = tableView.cellForRowAtIndexPath(indexPath)
let rect = tableView.convertRect(tableView.rectForRowAtIndexPath(indexPath), toView: tableView)


    var popoverContent = self.storyboard?.instantiateViewControllerWithIdentifier("loadZone") as! UIViewController
    var nav = UINavigationController(rootViewController: popoverContent)

    nav.modalPresentationStyle = UIModalPresentationStyle.Popover

    var popover = nav.popoverPresentationController

    popoverContent.preferredContentSize = CGSizeMake(100,200)
    popover!.sourceView = tableView
    popover!.sourceRect = rect

    self.presentViewController(nav, animated: true, completion: nil)

【讨论】:

以上是关于如何从 UITableViewCell.accessoryView 调用 PopOver 控制器?的主要内容,如果未能解决你的问题,请参考以下文章

如何将数据从回收器适配器发送到片段 |如何从 recyclerview 适配器调用片段函数

如何从 Firebase 获取所有设备令牌?

如何直接从类调用从接口继承的方法?

如何从服务器获取和设置 android 中的 API(从服务器获取 int 值)?如何绑定和实现这个

如何从Mac从android studio中的fabric注销? [复制]

如何从设备中获取 PDF 文件以便能够从我的应用程序中上传?