Chris Krogh
Chris Krogh

Reputation: 344

iOS TableView Row Deletion

When using the function editActionsForRowAt to delete the cells of a table, it requires the user to take action to delete (swiping and tapping or swiping fully across the cell) is there a way to call this without the user having to do anything?

Upvotes: 1

Views: 80

Answers (2)

totocaster
totocaster

Reputation: 6363

Yes you can call this method yourself from the code, but as mentioned in other answers you shouldn't call methods that are called by delegate owners.

Better way of doing this would be to extract contents of tableView(_:editActionsForRowAt) and call new extracted method from it and from anywhere you want too.

func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
    let removeAction = UITableViewRowAction(style: .destructive, title: "Delete", handler: { [weak self] action, indexPath in
        // Call extracted method
        self?.extractedAction(at: indexPath)
    })
    return [removeAction]
}

func extractedAction(at indexPath: IndexPath) {
    // Your old delete from old tableView(_:editActionsForRowAt) logic goes here.
}

func anyOtherPlaceInYourCode() {
    let indexPath = IndexPath(...) // which row?
    extractedAction(at: indexPath) 
}

Upvotes: 1

matt
matt

Reputation: 534893

As a matter of safety and style, you should never call a method that's a delegate method or other event owned by Cocoa.

But nothing stops you from "factoring out" that functionality, into a method that editActionsForRowAt calls and that you can call as well.

Upvotes: 0

Related Questions