在静态UITableViewCell中显示UIButton的弹出窗口

时间:2014-09-24 15:29:21

标签: uitableview swift uibutton popover

当我点击UIButton时,我有一个弹出窗口。此按钮位于静态UITableViewCell中。然而,当我按下按钮时,在UIButton下,popover不是我认为应该的地方。

因为它是一个静态单元格,我没有使用didSelectRowAtIndexPath,也不需要使用cellForRowAtIndexPath。我基本上使用UITableView作为在视图控制器中嵌入滚动视图的廉价替代方法。它主要用于数据输入。我有4个部分,每个部分有1行。我相信我需要弄清楚如何从每个部分的行中获取NSIndexPath以传递给Objectives C中与此类似的.sourceView

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
[self.profileItemsDetailPopover presentPopoverFromRect:cell.bounds inView:cell.contentView permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];


@IBAction func dateOfBirthAction(sender: AnyObject){

    var popoverViewController = self.storyboard?.instantiateViewControllerWithIdentifier("DateOfBirthViewController") as UIViewController
    popoverViewController.modalPresentationStyle = .Popover
    popoverViewController.preferredContentSize   = CGSizeMake(300, 300)

    let popoverPresentationViewController = popoverViewController.popoverPresentationController

    popoverPresentationViewController?.permittedArrowDirections = .Any
    popoverPresentationViewController?.delegate = self
    popoverPresentationViewController?.sourceView = tableView
    popoverPresentationViewController?.sourceRect = playerInformationBirthDateButton.frame

    presentViewController(popoverViewController, animated: true, completion: nil)
}

解决方案:

@IBAction func dateOfBirthAction(sender: UIButton){

    var cell = sender.superview!.superview! as UITableViewCell

    var popoverViewController = self.storyboard?.instantiateViewControllerWithIdentifier("DateOfBirthViewController") as UIViewController
    popoverViewController.modalPresentationStyle = .Popover
    popoverViewController.preferredContentSize   = CGSizeMake(300, 300)

    let popoverPresentationViewController = popoverViewController.popoverPresentationController

    popoverPresentationViewController?.permittedArrowDirections = .Any
    popoverPresentationViewController?.delegate = self
    popoverPresentationViewController?.sourceView = cell.contentView
    popoverPresentationViewController?.sourceRect = playerInformationBirthDateButton.frame

    presentViewController(popoverViewController, animated: true, completion: nil)
}

1 个答案:

答案 0 :(得分:2)

当您点击按钮时,您还可以找到“单元格”。

var cell = sender.superview!.superview! as UITableViewCell
// or the table
var table: UITableView = cell.superview as UITableView
// or the Indexpath
let indexPath = table.indexPathForCell(cell)