0

I found a way to programmatically select a row in my tableView through using the delegate method like suggested in this answer:

https://stackoverflow.com/a/40589905/3511695

Right now I have a .delete editingStyle option for each of my tableView's cells:

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    if (editingStyle == .delete) {
        // code that's called when user presses delete button
    }

How could I call this delete function programmatically? i.e. Simulate the user pressing the delete button? (Just like how I was able to programmatically simulate pressing a tableView cell)

I have an alertController in my if (editingStyle == .delete) {} that I need to have show up again, which is why I need to programmatically simulate the user pressing the Delete button in the UI rather than simply deleting the row.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
velkoon
  • 871
  • 3
  • 15
  • 35
  • 1
    The real answer is. Don't do this. You have have a model that you update. When you update the model, the view controller should automatically update the views. Don't treat your view as if it was a model. Use MVC or some other architecture and correctly partition your code. – Daniel T. Dec 09 '18 at 23:01

1 Answers1

1

What you're looking for is not UITableViewDelegate method, but UITableViewDataSource method.

This is not the best solution, but if you really want to do it like this, call this method on dataSource of your TableView and as parameters pass your TableView, editingStyle as .delete and certain IndexPath

tableView.dataSource?.tableView!(tableView, commit: .delete, forRowAt: indexPath)

Now what you should be doing:

You should put code from if statement in commit data source method to new method and then you should call this method here

if (editingStyle == .delete) {
    method()
}

and instead of calling tableView.dataSource?...

method()
Robert Dresler
  • 10,580
  • 2
  • 22
  • 40
  • I already have that code inside my `if (editingStyle == .delete) {}`. That just deletes the rows. I want to simulate the user pressing the `delete` button. (More is done in my code when the user presses the `delete` button than just deleting the row.) – velkoon Dec 09 '18 at 22:36
  • @velkoon yes, so just call what I wrote above – Robert Dresler Dec 09 '18 at 22:36
  • I have an alert controller that comes up in my `if (editingStyle == .delete) {}` that will not appear if I simply do `yourTableView.deleteRows(at: [someIndexPath], with: .automatic)`. I need the `if (editingStyle == .delete) {}` to be programmatically executed so my alertController appears. – velkoon Dec 09 '18 at 22:39
  • Perfect! I've got some more troubleshooting to do, but this is exactly what I needed, thank you – velkoon Dec 09 '18 at 23:19
  • Don't do this. Never directly call a delegate or data source method. If you think this is correct then you are doing it wrong. – rmaddy Dec 10 '18 at 15:27
  • Ah, of course, yes. Very easy to do it the correct way. I'm sure the information you provided originally will still prove resourceful to others too.. – velkoon Dec 11 '18 at 00:40