我有一个UITableView,我使用的方式与本教程在Xamarin上演示的方式相同。
根据示例中的说明,我将cell.Accessory设置为GetCell方法中的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总是空的,尽管我已经将cell.Accessory设置为DetailDisclosureButton。
答案列在这里表示设置cell.Accessory不影响cell.AccessoryView属性(我知道它处理ObjectiveC,但我认为这也适用于MonoTouch)。
相反,建议通过向DetailDisclosureButton分配UIButton来手动添加cell.AccessoryView。但是,这意味着我不能使用Xamarin示例中的AccessoryButtonTapped方法,需要创建自己的均衡器,等等。
作为另一种选择,我尝试过通过单元格的Subviews循环,并将弹出控制器连接起来如下所示:
uipoc.PresentFromRect (tableView.CellAt(indexPath).Subviews[1].Frame, tableView, UIPopoverArrowDirection.Right, true);然而,使用Subviews[0]和Subviews[2],它根本不起作用,而Subviews[1]给出了奇怪的结果--弹出菜单总是显示在表中第一个单元格的DetailDisclosureButton旁边,不管哪个单元格的按钮被点击。
我的问题是:是否有办法获得DetailDisclosureButton的视图(因此,附件的视图),如果cell.Accessory设置为UITableViewCellAccessory.DetailDisclosureButton的话。
如果不是,后一种解决方案(手动添加UIButton )是实现我想要的目标的唯一方法吗?我如何在C#/MonoTouch中做到这一点?
谢谢!
发布于 2012-10-08 14:02:28
经过一些吹毛求疵之后,我发现了一个有点丑陋但却在工作中的解决办法:
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左侧的DetailDisclosureButton,其箭头指向该单元格。显然,您需要为正确的维度减去(或添加) 40,这取决于您所选择的UIPopoverArrowDirection。
我怀疑这是不是正确的方法,但我想我会坚持下去,除非有人提出更好的建议。
https://stackoverflow.com/questions/12769034
复制相似问题