从通过设置 Cell.Accessory 添加的 DetailDisclosureButton 显示 UIPopoverController

Posted

技术标签:

【中文标题】从通过设置 Cell.Accessory 添加的 DetailDisclosureButton 显示 UIPopoverController【英文标题】:Show a UIPopoverController from a DetailDisclosureButton that is added by setting the Cell.Accessory 【发布时间】:2012-10-07 12:56:58 【问题描述】:

我有一个 UITableView,我使用的方式与 Xamarin 的 this tutorial 中演示的方式相同。

根据示例中的说明,我在GetCell 方法中将cell.Accessory 设置为DetailDisclosureButton,如下所示:

public override UITableViewCell GetCell (UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath)

     (Some code to create/style the cell, etc. See tutorial link.)
     cell.Accessory = UITableViewCellAccessory.DetailDisclosureButton;

我想要一个UIPopoverController 来显示DetailDisclosureButton 何时被点击。它的锚点需要“附加”到按钮上。

为此,我需要相应地更改AccessoryButtonTapped 方法,如下所示:

public override void AccessoryButtonTapped (UITableView tableView, NSIndexPath indexPath)

        uipoc = new UIPopoverController(uivc);
        uipoc.PopoverContentSize = new SizeF(200f, 300f);
        uipoc.PresentFromRect (tableView.CellAt (indexPath).AccessoryView.Frame, tableView, UIPopoverArrowDirection.Right, true);

Uipoc 是一个类变量,就像(仍然是空的)UIViewController uivc

据我了解,PresentFromRect 的第一个参数决定了 UIPopoverController “挂钩”到的 UIView,它需要该视图的 Frame 属性来执行此操作。

不幸的是,我上面的方法不起作用。 cell.AccessoryView 始终为 null,即使我已将 cell.Accessory 设置为 DetailDisclosureButton

答案listed here 表示设置cell.Accessory 不会影响cell.AccessoryView 属性(我知道它处理ObjectiveC,但我想这同样适用于MonoTouch)。

建议通过将UIButton分配给cell.AccessoryView来手动添加DetailDisclosureButton。但是,这意味着我不能使用 Xamarin 示例中的 AccessoryButtonTapped 方法,需要创建自己的事件处理程序,等等。

作为替代方案,我尝试通过单元格的 Subviews 循环,并像这样连接 popovercontroller:

uipoc.PresentFromRect (tableView.CellAt(indexPath).Subviews[1].Frame, tableView, UIPopoverArrowDirection.Right, true);

但是,使用Subviews[0]Subviews[2],它根本不起作用,而Subviews[1] 给了我奇怪的结果——弹出菜单总是显示在表格中第一个单元格的DetailDisclosureButton 旁边,没有无论点击哪个单元格的按钮。

我的问题: 如果只是将cell.Accessory 设置为UITableViewCellAccessory.DetailDisclosureButton,有没有办法获得DetailDisclosureButton 的视图(ergo,附件的视图)?

如果不是,后一种解决方案(手动添加UIButton)是实现我想要的唯一方法吗?如何在 C#/MonoTouch 中做到这一点?

谢谢!

【问题讨论】:

您需要弄清楚为什么AccessoryButtonTapped 不起作用。与 Xamarin 示例有什么不同?如果您在UITableViewCell 上设置了Accessory,而不是None,那么它应该会触发该消息。如果您运行 Xamarin 示例,它是否适合您? @jonathanpeppers AccessoryButtonTapped 有效,我对 Xamarin 示例没有任何问题。我的问题是,仅将cell.Accessory 中的UITableViewCellAccessory.DetailDisclosureButton 设置为GetCell 不会在任何地方公开附件的视图。 cell.AccessoryView 始终为 null,并且似乎与我为 cell.Accessory 选择的任何类型无关。不过,我确实需要配件的视图,以便将 UIPopoverController 连接到它。 您可以只使用RectangleF 来显示弹出框,您知道吗? @jonathanpeppers 是的,但这不会让我显示 UIPopupViewcontroller 的箭头指向 DetailDisclosureButton,因为我不知道按钮的位置。你的评论确实给了我一个想法:)。见下文。 【参考方案1】:

经过一番摆弄,我发现了一个有点难看但可行的解决方法:

public override void AccessoryButtonTapped (UITableView tableView, NSIndexPath indexPath)

    uipoc = new UIPopoverController(uivc);
    uipoc.PopoverContentSize = new SizeF(200f, 300f);
    uipoc.PresentFromRect (new RectangleF(cell.Frame.Right - 40f, cell.Frame.Y, cell.Frame.Width, cell.Frame.Height), tableView, UIPopoverArrowDirection.Right, true);

我不需要附件Frame 的位置信息,而是使用cell 本身的信息并从其右侧减去40。这将在该单元格中的DetailDisclosureButton 左侧显示UIPopupViewcontroller,其箭头指向它。显然,您需要为正确的维度减去(或添加)40,具体取决于您选择的UIPopoverArrowDirection

我怀疑这是正确的方法,但我想我会坚持下去,除非有人提出更好的建议。

【讨论】:

我认为这很好。可能不是一个简单的方法来抓住底层UIButton 而不循环通过Subviews。这似乎比试图在 UI 控件树中“找到”按钮更简洁。

以上是关于从通过设置 Cell.Accessory 添加的 DetailDisclosureButton 显示 UIPopoverController的主要内容,如果未能解决你的问题,请参考以下文章

WKWebView 添加为子视图

OctoberCMS:如何仅将 javascript 从组件添加到页面一次

如何以编程方式将视图从XML添加到布局?

通过 jquery 在 Bootstrap 4 轮播中添加类“活动”,从 mongodb 加载数据时无法正常工作

Axure初体验:简单交互通过按钮切换图片

如何在从Google Site添加时设置对Google云端硬盘文件/文件夹的权限