0

I have row actions and want the user to be able to click them, then be segued to the new VC along with the info for the object in the cell indexpath. This is my current code:

    func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
    let update = UITableViewRowAction(style: .normal, title: "Update") { (action, indexPath) in
        let cell = tableView.cellForRow(at: indexPath)
        self.performSegue(withIdentifier: "XXXXXX", sender: cell)
    }
    update.backgroundColor = UIColor.blue
    return [update]
}

My idea was that i can set cell as the indexpath the row action is on and then segue to the VC I want.

Issue is, obviously you cant draw a segue in IB from a row action, so how am i supposed to create this? I know i could make it so the user can select the cell row to be segued but i want it specifically on the row action not on the cell itself.

I appreciate any help with how this is achieved!

edit: to suggest how this Q is different from the proposed, Im not able to create the segue in storyboard for a row action to be able to reference it

jwarris91
  • 902
  • 9
  • 24
  • Possible duplicate of [IOS - How to segue programmatically using swift](http://stackoverflow.com/questions/27604192/ios-how-to-segue-programmatically-using-swift) – xoudini Jan 18 '17 at 21:31
  • I read that before posting, that answer still uses a Storyboard segue ID to segue im saying you cant create that in the first place for a row action? – jwarris91 Jan 18 '17 at 21:36
  • 1
    You can create it from the ViewController where belongs the tableView (and the cells), to the next one. Then call it. – Larme Jan 18 '17 at 21:38
  • You need to assign an identifier for the segue to be able to use it in `performSegue(withIdentifier:sender:)`. – xoudini Jan 18 '17 at 21:38

2 Answers2

1

Resolved, As commented you can create the segue in the IB at a VC level rather than on row actions specifically, then assign that segues id to the row actions using performSegue(withIdentifier:sender:) as proposed in my question.

Michael Fourre
  • 3,005
  • 3
  • 15
  • 26
jwarris91
  • 902
  • 9
  • 24
0

Create a segue from your current viewController to the destination viewController and give some segue id lets say "AbcVCto" And in your didSelectRowAtIndexPath- Call this method.

cell.tag = indexPath.row;
[self performSegueWithIdentifier:"AbcVCTo" sender:cell];

Above method will need prepare for segue and use it like this.

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if ([segue.identifier isEqualToString:@"AbcVCto"]) {
        UITableViewCell *cell = (UITableViewCell *)sender;
        //You can fetch data from array with your cell row number with cell.tag number
        //e.g
        segue.destinationViewController.selectedCellName = [arrayOfCellNames objectAtIndex:cell.tag];
    }
}

Above Pattern in Swift 3

cell.tag = indexPath.row
self.performSegue(WithIdentifier:"AbcVCTo", sender:cell);

override func prepare(for Segue: UIStoryboardSegue, sender: Any?) {
if forSegue.identifier == "ABCVCTo" {
      guard let cell = sender as? UITableViewCell else {
           return
      }
      //now your cell.tag is the selected cell indexNumber
 }

}

Syed Qamar Abbas
  • 3,637
  • 1
  • 28
  • 52