Add/Delete UITableViewCell with animation?

Objective CIos4Uitableview

Objective C Problem Overview


I know this might sound like a dumb question, but I have looked every where. How can I do this?

I know how to do this with a swype-to-delete method, but how cam I do it outside that function?

Please post some code samples.

Thanks!
Coulton

Objective C Solutions


Solution 1 - Objective C

[self.tableView beginUpdates];
[self.tableView insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationFade];
[self.tableView deleteRowsAtIndexPaths:deleteIndexPaths withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];

insertIndexPaths is an array of NSIndexPaths to be inserted to your table.

deleteIndexPaths is a array of NSIndexPaths to be deleted from your table.

Example array format for index paths :

NSArray *insertIndexPaths = [[NSArray alloc] initWithObjects:
		[NSIndexPath indexPathForRow:0 inSection:0],
		[NSIndexPath indexPathForRow:1 inSection:0],
		[NSIndexPath indexPathForRow:2 inSection:0],
	    nil];

Solution 2 - Objective C

In Swift 2.1+

tableView.beginUpdates()
tableView.deleteRowsAtIndexPaths([YourIndexPathYouWantToDeleteFrom], withRowAnimation: .Fade)
tableView.insertRowsAtIndexPaths([YourIndexPathYouWantToInsertTo], withRowAnimation: .Fade)
tableView.endUpdates()

And you can create an NSIndexPath easily like so:

NSIndexPath(forRow: 0, inSection: 0)

Solution 3 - Objective C

You want these two methods: insertRowsAtIndexPaths:withRowAnimation: and deleteSections:withRowAnimation: They are both detailed in the UITableView documentation.

Solution 4 - Objective C

Swift 4.0

    tableView.beginUpdates()
    tableView.deleteRows(at: [YourIndexPathYouWantToDeleteFrom], with: .fade)
    tableView.insertRows(at: [YourIndexPathYouWantToInsertTo], with: .fade)
    tableView.endUpdates()

For creating IndexPath use this:

IndexPath(row: 0, section: 0)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioniosfreakView Question on Stackoverflow
Solution 1 - Objective CSabobinView Answer on Stackoverflow
Solution 2 - Objective CChris KlinglerView Answer on Stackoverflow
Solution 3 - Objective CindragieView Answer on Stackoverflow
Solution 4 - Objective CAndrew VergunovView Answer on Stackoverflow