Friday, June 7, 2013

UITableView insertRowsAtIndexPaths withRowAnimation example in Objective C (iOS).


UITableView insertRowsAtIndexPaths withRowAnimation

Inserts rows in the receiver at the locations identified by an array of index paths, with an option to animate the insertion.

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

Parameters of [UITableView insertRowsAtIndexPaths withRowAnimation]
indexPaths
An array of NSIndexPath objects each representing a row index and section index that together identify a row in the table view.
animation
A constant that either specifies the kind of animation to perform when inserting the cell or requests no animation. See “Table Cell Insertion and Deletion Animation” for descriptions of the constants.

Discussion of [UITableView insertRowsAtIndexPaths withRowAnimation]
UITableView calls the relevant delegate and data source methods immediately afterwards to get the cells and other content for visible cells.

Note the behavior of this method when it is called in an animation block defined by the beginUpdates and endUpdates methods. UITableView defers any insertions of rows or sections until after it has handled the deletions of rows or sections. This happens regardless of ordering of the insertion and deletion method calls. This is unlike inserting or removing an item in a mutable array, where the operation can affect the array index used for the successive insertion or removal operation. For more on this subject, see “Batch Insertion and Deletion of Rows and Sections” in Table View Programming Guide for iOS.

UITableView insertRowsAtIndexPaths withRowAnimation example.
It's a two step process:

First update your data source so numberOfRowsInSection and cellForRowAtIndexPath will return the correct values for your post-insert data. You must do this before you insert or delete rows or you will see the "invalid number of rows" error that you're getting.

Then insert your row:

[tblSimpleTable beginUpdates];
[tblSimpleTable insertRowsAtIndexPaths:indexArray withRowAnimation:UITableViewRowAnimationRight];
[tblSimpleTable endUpdates];

Example of [UITableView insertRowsAtIndexPaths withRowAnimation].
-(void)insertMoreRows
{
    int row = [items count] - 1;
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:0];
    NSArray *indexPaths = [NSArray arrayWithObject:indexPath];
    [self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationTop];
}

UITableView insertRowsAtIndexPaths withRowAnimation example.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return [arr count];
    } else {
        return 1;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{  
    UITableViewCell *cell = (UITableViewCell*)[self.myTableView dequeueReusableCellWithIdentifier:@"DetailCell"];
    cell.textLabel.text=[arr objectAtIndex:indexPath.row];
    cell.backgroundColor=[UIColor clearColor];
    return cell;
}

-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    switch (section) {
        case 0:
            btnReleases=[UIButton buttonWithType:UIButtonTypeCustom];
            [btnReleases setFrame:CGRectMake(10, 0, 30, 39)];
            [btnReleases setImage:[UIImage imageNamed:@"buttonr.png"] forState:UIControlStateNormal];
            [btnReleases addTarget:self action:@selector(loadReleases) forControlEvents:UIControlEventTouchUpInside];
            return btnReleases;
            break;
        case 1:
            btnLinks=[UIButton buttonWithType:UIButtonTypeCustom];
            [btnLinks setFrame:CGRectMake(10, 0, 30, 39)];
            [btnLinks setImage:[UIImage imageNamed:@"buttonl.png"] forState:UIControlStateNormal];
            [btnLinks addTarget:self action:nil forControlEvents:UIControlEventTouchUpInside];
            return btnLinks;
            break;
        default:
            break;
    }

}
-(void)loadReleases
{

    [self.myTableView beginUpdates];
    [arr addObject:@"WTF"];
    NSArray *insert0 = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:1 inSection:0]];
    [self.myTableView insertRowsAtIndexPaths:insert0 withRowAnimation:UITableViewRowAnimationBottom];
    [self.myTableView endUpdates];
}

End of UITableView insertRowsAtIndexPaths withRowAnimation example article.