ICL1901
ICL1901

Reputation: 7778

Swift 3 indexPathForSelectedRow - getting null path

I'm trying to size a popover view in a segue. The popover works but is in the wrong position.

To set the position, I'm using the following code that tries to select the current IndexPathForSelectedRow, and then set the popover values.

It's not firing, and I am not sure why.

The code I'm using is as follows:

 override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        
        if segue.identifier == "RecordToNotes" {
           
            if let path = detailsTable.indexPathForSelectedRow {
                let cellFrame = detailsTable.rectForRow(at: path)
               
                let vc = segue.destination
                vc.preferredContentSize = CGSize(width: 400, height: 400)
                let controller = vc.popoverPresentationController
                controller?.delegate = self
                controller?.sourceView = detailsTable
                controller?.sourceRect = CGRect(x: cellFrame.midX + 50, y: cellFrame.midY, width: 0, height: 0)
                controller?.permittedArrowDirections = .right
            }
            
        }
        
    }

The path returns nil.

Update

I setup the cell in cellAtRow. Here's that snippet:

case 2: //notes
                cell.detailPickerButton.isHidden = false
                cell.detailPickerButton.isEnabled = true
                
                cell.detailPickerButton.addTarget(self, action: #selector(detailsViewController.showNotesPanel(_:)), for: UIControlEvents.touchUpInside)
           ....

I am not overriding didSelectRow. Should I?

Upvotes: 1

Views: 186

Answers (1)

Reinier Melian
Reinier Melian

Reputation: 20804

Try translating the frame to your ViewController.View coordinates

use this line

controller?.sourceRect = self.view.convert(cellFrame, from: detailsTable)

instead of

controller?.sourceRect = CGRect(x: cellFrame.midX + 50, y: cellFrame.midY, width: 0, height: 0)

Hope this helps

Upvotes: 1

Related Questions