LucyEly
LucyEly

Reputation: 63

not able to figure out what sender to return from a leadingSwipeActions for a prepare for segue

Sorry, I am brand new swift and IOS development but I have a table view with each element in the table view I can tap on the item to go to my edit screen which determines the information in the table cell. I would like to also have a leadingSwipeActionsConfigurationForRowAt bring up an edit button that uses the same segue way to get to into edit screen as taping the cell. The issue is that my prepare(for segue:) statement for the handling of the tap segue keeps failing because of the sender of leadingSwipeAction. Now I am really confused about what I should put as my sender in my leadingSwipeAction. I have tried a UITableViewCell() which failed this guard statement:

guard let ViewController = segue.destination as? ViewController else { fatalError("Unexpected destination: \(segue.destination)") }

for being a UIView() and then I tried sending a taskCellTableViewCell which is the object/class I called an individual cell but that returned a nil/empty cell which failed another guard statement in the prepare. So now I am confused about how to get and send the actual cell that leadingSwipeAction was performed on. I hope anyone can help me figure out the correct sender I need to return to pass the prepare. Thanks

Here is the code used for the leadingSwipeAction and prepare:

//leading swipe for editing code
override func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {

    let edit = UIContextualAction(style: .normal, title: "Edit") { (contextualAction, view, actionPerformed: (Bool) -> ()) in
        self.performSegue(withIdentifier: "showDetail", sender: UITableViewCell())
        actionPerformed(true)

    }

    return UISwipeActionsConfiguration(actions: [edit])
}

    // Get the new view controller using segue.destination.
    // Pass the selected object to the new view controller.
    super.prepare(for: segue, sender: sender)

    switch(segue.identifier ?? "") {

        case "addItem":
        os_log("Adding a new task.", log: OSLog.default, type: .debug)

        case "showDetail":
        guard let ViewController = segue.destination as? ViewController else {
            fatalError("Unexpected destination: \(segue.destination)")
        }

        guard let selectedTaskCell = sender as? taskCellTableViewCell else {
            fatalError("Unexpected sender: \(sender)")
        }

        guard let indexPath = tableView.indexPath(for: selectedTaskCell) else {
            fatalError("The selected cell is not being displayed by the table")
        }

        let selectedTask = tasks[indexPath.row]
        ViewController.Task = selectedTask

        default:
        fatalError("Unexpected Segue Identifier; \(segue.identifier)")

    }
}

it fails trying to use the showDetail segue again.

Also here is a pastebin of the complete code of the project pastebin

Upvotes: 0

Views: 88

Answers (2)

LucyEly
LucyEly

Reputation: 63

Ok after searching around and changing my google search using every buzz word I can think of I finally found the answer. To make the sender be the current cell that the LeadingSwipeAction is performed on the sender needs to set to tableView.cellForRow(at: indexPath) which takes the tableView being passed into the LeadingSwipe func and gets cell form the indexPath.

Upvotes: 0

Alec.
Alec.

Reputation: 5525

I don't think a tableViewCell can be used as a sender, are you not able to set this to the ViewController you are initiating the segue from, that should work.

Upvotes: 0

Related Questions