0

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

Eyal
  • 10,777
  • 18
  • 78
  • 130

1 Answers1

2

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

Community
  • 1
  • 1
Alex Muller
  • 1,565
  • 4
  • 23
  • 42
  • If this helped you, please select it as the answer. – Alex Muller Apr 25 '12 at 00:22
  • The thing is that I want to do this before any rows are in the table view, but the problem is that the table view doesn't know yet how many sections or how many rows in a section because those delegate methods didn't called yet, because I didn't do reloadData... – Eyal Apr 25 '12 at 09:47