UITableView add cell Animation

40,495

Solution 1

You can use the following UITableView method:

- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation

Example with self.dataSource being a mutable array and your table only having 1 section:

[self.dataSource addObject:@"New Item"];
NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:[self.dataSource count]-1 inSection:0];
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];

Note: Subtracted 1 from datasource count since NSIndexPath is zero indexed.

Solution 2

Swift

Example array that is the data source for the table view

var myArray = ["Cow", "Camel", "Sheep", "Goat"]

The following will add a row with animation at the top of the table view.

// add item to current array
myArray.insert("Horse", atIndex: 0)

// insert row in table
let indexPath = NSIndexPath(forRow: 0, inSection: 0)
tableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Fade)

Multiple updates

If you need to do multiple insertions and/or deletions then surround them with beginUpdates() and endUpdates().

tableView.beginUpdates()
tableView.insertRowsAtIndexPaths([addIndexPath1, addIndexPath2, ...], withRowAnimation: .Fade)
tableView.deleteRowsAtIndexPaths([deleteIndexPath1, deleteIndexPath2, ...], withRowAnimation: .Fade)
tableView.endUpdates()

Further reading

Share:
40,495
Ameya
Author by

Ameya

I am Architect, expertise in J2EE , iPhone and Android, Sencha Touch, Phone Gap and ExtJS development.

Updated on November 18, 2020

Comments

  • Ameya
    Ameya over 3 years

    Can any one help me out with UITableView animating issue?

    By default we have animation for deleting cell and reordering cells in UITableView.

    Can we have animated adding cell, if so how to do it.

    I have checked out Three20, did not not get how twitter has done the table expand animation under MyProfile>ReTweets.

    Want to try it without Three20 frameowrk, using the existing animation in UITableView.

  • Ameya
    Ameya almost 14 years
    thanks for the code, can u give me a working sample ? o I am slightly confused with how to update the data source while doing the insertion.
  • Ameya
    Ameya almost 14 years
    earlier I would be simply adding a record to data source (NSMutableArray) and calling [self.tableView reloadData]; But that would not animatedly bring in the new element .
  • Ameya
    Ameya almost 14 years
    just a small change in ur code @JK plz edit your answer , hope it helps some one looking for this kind of solution. [tableView insertRowsAtIndexPaths:[NSArray arrayWithObjects:[NSIndexPath indexPathForRow:[self.dataSource count] inSection:0],nil] withRowAnimation:UITableViewRowAnimationRight];
  • SwiftArchitect
    SwiftArchitect about 10 years
    Do not forger to surround with [self.tableView beginUpdates] and [self.tableView endUpdates]
  • KlimczakM
    KlimczakM over 9 years
    Rows are enumerated from 0, so new index = self.dataSource.count will cause an exception, it should be NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:self.dataSource.count -1 inSection:0];. @Run Loop please edit your answer
  • Return-1
    Return-1 almost 7 years
    I am using your code to insert the rows i need inserted however, no matter what animation type i pick it always animates in the same way (i.e fade, botom ,left, all have the same behaviour). Any ideas why this happens?
  • Suragch
    Suragch almost 7 years
    @GeorgeAvgoustis, I haven't worked on this for a while so I don't know. I'll put it on my list of things to check out, but I might not get to it for a while. If you figure it out first, please leave another comment.