Show popover for UIButton in static UITableViewCell

I have a popover when I click on the UIButton. This button is in a static UITableViewCell. When I click on the button, the popover is not where I would think it should be, under the UIButton.

Since this is a static cell, I am not using didSelectRowAtIndexPath and I am not using cellForRowAtIndexPath. I mostly use UITableView as a cheap alternative to embedding scrolling in my controller. This is essential for data entry. I have 4 sections and each section has 1 row. I believe I need to figure out how to get the NSIndexPath from the string in each section to navigate to the .sourceView, something similar to this in Objective-C

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)
}

      

Decision:

@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)
}

      

+3


source to share


1 answer


you can also recognize the "Cell" when you click on the button.



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)

      

+2


source







All Articles