我有一个UITableView,我使用它的方式与本教程在Xamarin演示的方式相同。
根据示例中的说明,我在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;
}
我想在点击DetailDisclosureButton
时显示一个UIPopoverController
。它的锚点需要"附加"到按钮上。
为了做到这一点,我需要相应地更改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
始终为空,即使我已将cell.Accessory
设置为DetailDisclosureButton
。
这里列出的答案表明设置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
的视图(即附件的视图)?
如果不是,后一种解决方案(手动添加UIButton
)是实现我想要的唯一方法吗?我如何在C#/MonoTouch中做到这一点?
谢谢!
经过一番摆弄,我发现了一个有点丑陋但有效的解决方法:
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
。
我怀疑这是正确的方法,但我想除非有人提出更好的建议,否则我会坚持下去。