Ghost108 Ghost108 -3 years ago 175
Swift Question

swift insert row with animation effect

In the first draft of my app, I filled my NSTableView with a static array.
I had a NSButton, which inserts a new row with an animation effect like this:

tableView.insertRows(at: IndexSet(integer: tableview.numberOfRows), withAnimation: .effectFade)

Short question at this point:
Do I need the begin-/endUpdates() command?
I see now difference if I use this code or not.

Primary Problem:

Now my second draft:

Now I fetch my data for the TableView from Core Data.
If I click on the NSButton (to insert a new row) I do something different: I create a new record, save it into core data, fetch all data again and reload my TableView.

This works fine, but I can't use this line anymore in my second draft.

tableView.insertRows(at: IndexSet(integer: tableview.numberOfRows), withAnimation: .effectFade)

Anybody an idea how can I use this "beautiful" animation with core data again?

Answer Source

Short question: You don't need beginUpdates() / endUpdates() for a single insert/delete/move operation, it's only needed for multiple simultaneous operations.

Primary problem: reloadData() doesn't show any animation. Instead of ...fetch all data again and reload my TableView insert the object in the table view with insertRows(at. It updates the table view showing the animation.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download