Eyal
Eyal

Reputation: 10818

Objective c - Is there a way to populate a table view with some animation?

I have a view controller with a table view and a NSArray that represent my model. In viewDidLoad I fill the array with objects and then I call [tableView reloadData] to fill the table view. The result is that the table view get filled in one chunk, and I don't like it.
I want it to slide down, like I was adding one cell after a nother.
Is this possible?

If I try to do something like this:

    [self.tableView beginUpdates];

    for (int i=0; i < sortedPersons.count; i++) {
        Person *personInfo = [sortedPersons objectAtIndex:i];
        [self.myMainModelArr addObject:personInfo];

        NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:i inSection:0];
        [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
    }

    [self.tableView endUpdates];

I get Invalid table view update error

Upvotes: 0

Views: 284

Answers (1)

Alex Muller
Alex Muller

Reputation: 1565

When using UITableView, there are a few delegate functions that you can use that should assist with this. When you about to add something to the tableView, be sure to use:

[tableView beginUpdates];

Then when inserting you can use the delegate method:

- (void)insertSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation

and then when finishing up the adding, be sure to use:

[tableView endUpdates];

All of these functions are viewable from the UITableView Class Reference

There is a slight snippet of code here: UITableView add cell Animation

Upvotes: 2

Related Questions