0

I'm trying to access the IndexPath of the selected cell of an UITableView, but it isn't in the same ViewController. How can I achieve this?

 func tableView(_ tableView: UITableView, accessoryButtonTappedForRowWith indexPath: IndexPath) {

    mainTable.selectRow(at: indexPath, animated: true, scrollPosition: UITableViewScrollPosition.none)
    let popController = UIStoryboard(name: "Maina", bundle: nil).instantiateViewController(withIdentifier: "popoverId") as! SidePopOverViewController


    popController.modalPresentationStyle = UIModalPresentationStyle.popover

    popController.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection.left
    popController.popoverPresentationController?.delegate = self
    popController.popoverPresentationController?.sourceView = mainTable.cellForRow(at: mainTable.indexPathForSelectedRow!)
    popController.popoverPresentationController?.sourceRect = (mainTable.cellForRow(at: mainTable.indexPathForSelectedRow!)!.bounds)

    self.present(popController, animated: true, completion: nil)    
}


func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle {  
    return UIModalPresentationStyle.none
}
Abisanth11
  • 15
  • 5
  • explain it little more. – Dharmesh Kheni Dec 27 '17 at 12:55
  • I have Popover (from UITableViewCell, anchor: accessoryButton) where I have multiple buttons. One of which is to rename the Cell, but I need the IndexPath of this cell to do so. The problem is, that the UITableView and the Popover aren't in the same ViewController and I can't access the IndexPath of the selected row. – Abisanth11 Dec 27 '17 at 13:18
  • add code snippet for the details – Ganesh Manickam Dec 27 '17 at 13:44
  • Since you are filling several properties of the popover VC already, I would create a IndexPath var and fill it as well – Daniel Dec 27 '17 at 13:53
  • @Daniel I'll get this error message if I put mainTable.indexPathForSelectedRow outside of a function: Cannot use instance member 'mainTable' within property initializer; property initializers run before 'self' is available – Abisanth11 Dec 27 '17 at 13:56
  • 1
    How about saving the tapped index path as a property of the table view controller, putting the rename function inside the table view controller, and using the delegate reference to call rename? (It seems more logical to me that the TVC should manage its cells rather than passing that responsibility to the popover.) – Phillip Mills Dec 27 '17 at 14:13

0 Answers0