3
votes

I have a popover when I tap on a UIButton. This button is in a static UITableViewCell. Howeever when I tap on the button, the popover isn't where I would think it should, under the UIButton.

Because it's a static cell I am not using didSelectRowAtIndexPath nor do I need to use cellForRowAtIndexPath. I'm basically using the UITableView as a cheap alternative to embedding a scroll view inside my view controller. It's essentially 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 row in each section to pass 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)
}

Solution:

@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
thanks..it works for me as well..How do i give pointer on exact cell?dhaval shah

1 Answers

2
votes

you could also find out 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)