-1

So I have a table view and a Custom VC. then I have an object exercises with detailImage as property. How can I get the indexPath.row from the table row action into my prepareForSegue function?

this returns nil: self.tableView.indexPathForSelectedRow

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "detailsSegue" {            
          if let indexPath = self.tableView.indexPathForSelectedRow {
             let destinationController = segue.destinationViewController as! DetailViewController
             print(self.exercises[indexPath.row].image)
             destinationController.detailImage = self.exercises[indexPath.row].image
             print ("send")
           }
    }
}
Victor Sigler
  • 23,243
  • 14
  • 88
  • 105
arvidurs
  • 2,853
  • 4
  • 25
  • 36
  • You don't with table row actions. You handle it with the action itself. See my example here http://stackoverflow.com/a/27856196/4339121 at the bottom is a swift version – soulshined Sep 03 '15 at 21:43

2 Answers2

1

Instead of this in your code:

if let indexPath = self.tableView.indexPathForSelectedRow {

Try this:

if let indexPath = self.tableView.indexPathForSelectedRow() {
Rich G
  • 36
  • 5
  • I get " Invalid use of "()" to call a value of non-function type "NSIndexPath" and the build fails. – arvidurs Sep 03 '15 at 22:44
  • Is the Custom VC the DataSource and Delegate for the Table View? – Rich G Sep 03 '15 at 23:54
  • From the main VC where the editActionAtRow resides, it has DataSource and Deletate connected to that table view. – arvidurs Sep 04 '15 at 10:15
  • A few more questions - Which version of Swift / Xcode are you using? Is the main VC a subclass of UITableViewController, or UIViewController? If it's UIViewController, have you made an Outlet for the tableView? – Rich G Sep 05 '15 at 14:22
  • I use the latest beta Version 7.0 beta 6 (7A192o) Its a subclass of UITableViewController. Sorry for being so noob about it... – arvidurs Sep 07 '15 at 20:09
  • It seems like for some reason it's not recognizing "self" as a UITableViewController. Try this: 1) Connect the TableView in interface builder to an IBOutlet 2) Instead of calling self.tableView.indexPathForSelectedRow, use the new outlet name instead of tableView. For example - create an outlet for the TableView named myTable, then in the segue use --- if let indexPath = self.myTable.indexPathForSelectedRow() {} – Rich G Sep 07 '15 at 20:45
  • So I in the main swift file I created an outlet myTable and connected the TableView from main VC with that outlet. In the swift file, I did as you saig: if let indexPath = self.myTable.indexPathForSelectedRow() {} and got the same error as before: Invalid use of "()" to call a value of non-function type "NSIndexPath". Not sure what else I can do. http://oi61.tinypic.com/2hojw2h.jpg – arvidurs Sep 07 '15 at 21:16
  • Without seeing the whole code for the class, I wouldn't know where else to look. – Rich G Sep 07 '15 at 22:26
  • For me removing the parens worked, for iOS 9. `if let index = tableView.indexPathForSelectedRow?.row {}` – alana314 Jan 27 '16 at 22:14
0

After playing around with the sender object in editActionsForRowAtIndexPath I got it to work. It's maybe a dirty solution but it works.

override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? {

    let detailsAction = UITableViewRowAction(style: .Default, title: "Details", handler: {(action:UITableViewRowAction!, indexPath:NSIndexPath!) -> Void in
        self.performSegueWithIdentifier("detailsSegue", sender: indexPath) //sender is the indexPath
        }
    )

and then in the prepareForSegue

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "detailsSegue" {
        let indexPath = sender!
        let destinationController = segue.destinationViewController as! DetailViewController
        print(self.exercises[indexPath.row].image)

There is probably a better way..Anyone to comment?

Community
  • 1
  • 1
arvidurs
  • 2,853
  • 4
  • 25
  • 36