2015-02-08 28 views
3

我有一個表格視圖,並且我使用UITableViewRowAction向用戶提供了兩個選項,當在一行上執行滑動操作時,'刪除'和'更多',我希望更多地顯示彈出窗口。 popover需要知道它所錨定的視圖,那麼如何獲得對「更多」按鈕的引用?如何從UITableViewRowAction獲取對按鈕的引用?

我的代碼:

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]; 
} 

此代碼示出了如在錨定小區整體,而不是在「更多」按鈕的酥料餅。

我已經搜索了關於UITableViewRowAction的文檔,但是沒有任何內容讓我領先。 action參數的類型也是UITableViewRowAction,所以我不確定它可以被使用。


更新

根據該意見,我得到了它的工作。我在自定義單元格視圖控制器中調用方法,在那裏訪問子視圖數組。我不喜歡訪問子視圖數組,並假定該數組中的按鈕的位置。

工作代碼:

在tableview中的回調我做的:

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我做的:

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,但它是零。

所以我還是想一個更好的辦法讓「刪除確認意見」,這是我現在訪問self.subviews[0](第二subview[1]在我看來是可行的,因爲我決定它的位置,當我回到[deleteRowAction, moreRowAction]

+0

你嘗試顯示moreRowAction按鈕查看aPopover.presentPopoverFromRect(popRect,inView:moreRowAction,permittedArrowDirections:UIPopoverArrowDirection.Any,動畫:真) – Shashi3456643 2015-02-08 10:26:17

+0

它沒有繼承'UIView',所以這是行不通的。我只是嘗試了一些錯誤,我不能使用變量來定義自己,但由於第一個原因,它不應該起作用。 – 2015-02-08 10:34:53

+1

也許你可以找到更多的按鈕的座標位置,並嘗試在UITableViewCell中顯示 – Shashi3456643 2015-02-08 10:49:48

回答

1

似乎沒有任何直接訪問按鈕本身的方法,但是在您滑過單元格後,我相信會發生什麼,是單元格被移動到左側,並且一個名爲UITableViewCellDeleteConfirmationView的新視圖添加到所以,如果將彈出窗口放在單元格的原點加上其寬度,它將位於單元格的右邊緣,這將位於按鈕的左側(假設您的按鈕是最左邊的那個按鈕有多個但是噸)。

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) 
    }); 
+0

好主意。但我認爲我會堅持最終的子視圖[0] ...無論如何+1 :) – 2015-02-08 18:35:39

+0

不幸的是,似乎cell.frame.origin.x始終是0 – Kremk 2016-02-25 10:41:14

相關問題