How can we dynamically add a new cell between two given cells in a TableView?

I need to add a new cell to select a table cell. The new cell should be below the selected cell. Can we do this?

Below is the code in which I create 4 cells and want to insert a cell between 2 and 3.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
cell= [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];


if(indexPath.row == 0){

    cell.textLabel.text = @"My Tweets";
    cell.textLabel.textColor = [UIColor whiteColor];
    cell.backgroundColor = [UIColor blackColor];
    cell.imageView.image = [UIImage imageNamed:@"tweets.png"];
}

if(indexPath.row == 1){

    cell.textLabel.text = @"Search";
    cell.backgroundColor = [UIColor blackColor];

    cell.textLabel.textColor = [UIColor whiteColor];
    cell.imageView.image = [UIImage imageNamed:@"search.png"];
}

if(indexPath.row == 2){

    cell.textLabel.text = @"Followers";
    cell.textLabel.textColor = [UIColor whiteColor];
    cell.backgroundColor = [UIColor blackColor];
    cell.imageView.image = [UIImage imageNamed:@"followers.png"];
}

if(indexPath.row == 3){

    cell.textLabel.text = @"Following";
    cell.textLabel.textColor = [UIColor whiteColor];
    cell.backgroundColor = [UIColor blackColor];
    cell.imageView.image = [UIImage imageNamed:@"following.png"];
}



return cell;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

//here I have to insert a new cell below on the click this existing cell.


if(indexPath.row == 1)
{

    NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:indexPath.row+1 inSection:0];
    NSArray *indexPaths = [NSArray arrayWithObject:newIndexPath]; 

    [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPaths] withRowAnimation:UITableViewRowAnimationAutomatic];
}

}

+3
source share
3 answers

You must update your data source and then call this UITableView method:

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

It will cause

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

table view data source method and update the table by selecting the selected animation.

You would not create a new cell in the didSelectRowAtIndexPath method, as you do in the code you posted.


edited to add information.

indexPaths ( ), :

NSInteger row = [indexPath row];
NSInteger section = [indexPath section];    
NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:row+1 inSection:section];
NSArray *indexPaths = [NSArray arrayWithObject:indexPath]; 
+1

indexpath.row + 1 insertrowatindexpath tableview......... aloso reload table, .

, .

    - (void)setEditing:(BOOL)editing animated:(BOOL)animated
    {
        [super setEditing:editing animated:animated];

    NSArray *paths = [NSArray arrayWithObject:
                            [NSIndexPath indexPathForRow:1 inSection:0]];
        if (editing)
        {
            [[self tableView] insertRowsAtIndexPaths:paths
                                    withRowAnimation:UITableViewRowAnimationTop];
        }
        else {
           //Your Operation [[self tableView] deleteRowsAtIndexPaths:paths
                                    withRowAnimation:UITableViewRowAnimationTop];
        }
[tableView reloadData];
    }

, UITableview else, tutorial

, . , : -

: -

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];

, , insertRowsAtIndexPaths: withRowAnimation: .

+1

Just call delegates insertrowsatindexpath at the start of the update cycle and complete the update. For example, try doing this as follows:

[tableViewForFinishRace beginUpdates];
        NSIndexPath *newIndexPath = [NSIndexPath    indexPathForRow:arrayForTitle.count-1 inSection:0];
        NSLog(@"index path for insertion =%@",newIndexPath);
        [tableViewForFinishRace insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationTop];
        [arrayForTitle insertObject:@"Gearing Up for a" atIndex:arrayForTitle.count-1];
        [arrayForImage insertObject:@"gearing-up-icon.jpeg" atIndex:arrayForTitle.count-2];
[tableViewForFinishRace endUpdates];
+1
source

All Articles