如何从 UITableViewRowAction 获取对按钮的引用? [英] How to get a reference to button from UITableViewRowAction?

查看:32
本文介绍了如何从 UITableViewRowAction 获取对按钮的引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表格视图,我使用 UITableViewRowAction 在一行上完成滑动时向用户显示两个选项,删除"和更多",我希望显示更多一个弹出框.弹出框需要知道它所在的视图,那么我如何获得对显示更多"的按钮的引用?

I have a table view and I use UITableViewRowAction to present two options to the user when a swipe is done on a row, 'delete' and 'more', and I want the more to show a popover. The popover needs to know the view it is anchored at, so how do I get a reference to the button which says 'more'?

我的代码:

func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]? {
    var moreRowAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "More", handler:{action, indexpath in
        
        let p = SomeViewController(aClient: client)
        let aPopover = UIPopoverController(contentViewController: p)
        let popRect = self.view.frame
        // What should x be?
        let x: UIView = self.tableView.cellForRowAtIndexPath(indexPath)!
        aPopover.presentPopoverFromRect(popRect, inView: x, permittedArrowDirections: UIPopoverArrowDirection.Any, animated: true)

    });

    var deleteRowAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Delete", handler:{action, indexpath in
        // Do something to delete.
    });
    
    return [deleteRowAction, moreRowAction];
}

此代码将弹出框显示为锚定在整个单元格处,而不是更多"按钮处.

This code shows the popover as anchored at the whole cell, not at the 'more' button.

我搜索了有关 UITableViewRowAction 的文档,但没有任何内容给我提供线索.action 参数的类型也是 UITableViewRowAction 所以我不确定它是否可以使用.

I have searched the documentation about UITableViewRowAction, but nothing there gave me a lead. The type of the action argument is also UITableViewRowAction so I'm not sure it can be used.

根据评论,我得到了它的工作.我在自定义单元格视图控制器中调用一个方法,在那里我访问子视图数组.我不喜欢访问 subviews 数组并假设按钮在该数组中的位置.

According to the comment, I got it working. I call a method in the custom cell view controller, there I access the subviews array. I don't like accessing subviews array and assuming the position of the button in that array.

工作代码:

在 tableview 回调中我做:

In the tableview callback I do:

func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]? {
    var moreRowAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Plus", handler:{action, indexpath in
        
        let cell = self.tableView.cellForRowAtIndexPath(indexPath)! as MyCell
        cell.showPopover()
        
    });

在 MyCell 中,我这样做:

Inside MyCell I do:

func showPopover() {
    let p = ClientLongPressViewController(client: self.client)
    let aPopover = UIPopoverController(contentViewController: p)
    let x: UIView = self.subviews[0].subviews[1] as? UIView ?? self
    let popRect = x.frame
    aPopover.setPopoverContentSize(CGSize(width: 215, height: 241), animated: true)
    aPopover.presentPopoverFromRect(popRect, inView: x, permittedArrowDirections: UIPopoverArrowDirection.Any, animated: true)
}

我尝试(在 MyCell 中)访问 self.contentView 但这仅包含来自主单元格视图的元素,而不包含滑动元素.
也尝试使用 self.editingAccessoryView,但它是零.

I tried (inside MyCell) to access self.contentView but this only contains elements from the main cell view, not the swiping elements.
Also tried using self.editingAccessoryView, but it is nil.

所以我仍然想要一种更好的方式来获得删除确认视图",我现在使用 self.subviews[0](第二个 subview[1] 在我看来是可以的,因为我在返回 [deleteRowAction, moreRowAction] 时决定了它的位置.

So I would still like a nicer way to get the "delete confirmation view", which I now access with self.subviews[0] (The second subview[1] is okay in my opinion because I decide it's position when I return [deleteRowAction, moreRowAction].

推荐答案

似乎没有任何直接的方式访问按钮本身,但是在您滑动单元格之后,我相信会发生什么,就是单元格向左移动,并在右侧添加了一个名为 UITableViewCellDeleteConfirmationView 的新视图.因此,如果您将弹出框放置在单元格的原点加上其宽度处,它将位于单元格的右边缘,该边缘将位于按钮的左侧(假设您的按钮是最左边的,如果您有多个按钮).

There doesn't seem to be any straight forward way access the button itself, but after you swipe the cell, I believe that what happens, is that the cell is moved to the left, and a new view called the UITableViewCellDeleteConfirmationView is added on the right. So, if you position the popover at the cell's origin plus its width, it will be at the right edge of the cell, which will be just to the left of your button (assuming that your button is the left most one if you have multiple buttons).

var moreRowAction = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "More", handler:{action, indexpath in

    let p = SomeViewController(aClient: client)
    let aPopover = UIPopoverController(contentViewController: p)
    let cell: UITableViewCell = self.tableView.cellForRowAtIndexPath(indexPath)!
    let popRect = CGRectMake(cell.frame.origin.x + cell.frame.size.width, cell.frame.size.height/2.0, 1, 1);
    aPopover.presentPopoverFromRect(popRect, inView: cell, permittedArrowDirections: .Right, animated: true)
    });

这篇关于如何从 UITableViewRowAction 获取对按钮的引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆