How can I remove rows from a UITableView with animation?

I am having problems deleting rows from a table view. I am using below code when delete button was clicked in line:

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:control.tag-100 inSection:0];
[resultList removeObjectAtIndex:indexPath.row];
[resultView beginUpdates];
[resultView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
[resultView endUpdates];
//[resultView reloadData];

      

The first line was deleted successfully, but then the indices were incorrect. So when I remove the last row it gives an out of bounds index exception.

Cell Generation Code:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"personalizeTableCell";

    PersonalizeCell *cell = (PersonalizeCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
        cell = [[PersonalizeCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];        
    cell.title.text = @"text";
    cell.rateView.tag = indexPath.row + 100;
    return cell;
}

      

Where am I going wrong?

UPDATE:

    for (NSInteger j = 0; j < [venuesTableView numberOfSections]; ++j)
    {
        for (NSInteger i = 0; i < [venuesTableView numberOfRowsInSection:j]; ++i)
        {
           PersonalizeCell* cell = (PersonalizeCell*)[venuesTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:j]];
           cell.rateView.tag = 100 + i;
        }
    }

      

solved my problem. Thanks to Nenad M.

+3


source to share


1 answer


Assuming yours is [NSIndexPath indexPathForRow:control.tag-100 inSection:0];

returning the correct index path .... Have you tried moving the call removeObjectAtIndex

inside the "start / end update" bracket ?:

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:control.tag-100 inSection:0];
[resultView beginUpdates];
[resultList removeObjectAtIndex:indexPath.row];
[resultView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
[resultView endUpdates];

      

UPDATE:

Obviously yours cell.rateView.tag

will become incorrect after you delete the first cell. So after every delete (i.e. every removeObjectAtIndex...

) you have to iterate over the remaining tableview cells to re-assign the correct tag value ( cell.rateView.tag = indexPath.row + 100

)! Otherwise, yours [NSIndexPath indexPathForRow:control.tag-100 inSection:0];

will return the wrong index patch, therefore leading to your error out of bounds!

Reassigning tag values:

You don't need to reload the whole table, just iterate over the remaining cells reassigning the tag value after [resultView endUpdates];

:

NSMutableArray *cells = [[NSMutableArray alloc] init];
for (NSInteger j = 0; j < [tableView numberOfSections]; ++j)
{
    for (NSInteger i = 0; i < [tableView numberOfRowsInSection:j]; ++i)
    {
        [cells addObject:[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:j]]];
    }
}

      

Now run:

for (PersonalizeCell* cell in cells)
{
    cell.rateView.tag = // new calculated tag
}

      



(Or re-assignment even in the inner loop of the first piece of code).


Here is some really typical code for the whole process, two lines from the table in the example:

Please note that facebookRowsExpanded is a class variable that you must have:

if ( [theCommand isEqualToString:@"fbexpander"] )
{
NSLog(@"expander button......");
[tableView deselectRowAtIndexPath:indexPath animated:NO];

NSArray *deleteIndexPaths;
NSArray *insertIndexPaths;

facebookRowsExpanded = !facebookRowsExpanded;
// you must do that BEFORE, not AFTER the animation:

if ( !facebookRowsExpanded ) // ie, it was just true, is now false
    {
    deleteIndexPaths = [NSArray arrayWithObjects:
            [NSIndexPath indexPathForRow:2 inSection:0],
            [NSIndexPath indexPathForRow:3 inSection:0],
             nil];
    [tableView beginUpdates];
    [tableView
        deleteRowsAtIndexPaths:deleteIndexPaths
        withRowAnimation: UITableViewRowAnimationMiddle];
    [tableView endUpdates];
    }
else
    {
    insertIndexPaths = [NSArray arrayWithObjects:
            [NSIndexPath indexPathForRow:2 inSection:0],
            [NSIndexPath indexPathForRow:3 inSection:0],
             nil];
    [tableView beginUpdates];
    [tableView
        insertRowsAtIndexPaths:insertIndexPaths
        withRowAnimation: UITableViewRowAnimationMiddle];
    [tableView endUpdates];
    }

// DO NOT do this at the end: [_superTableView reloadData];
return;
}

      

NOTE: your code for numberOfRowsInSection must use facebookRowsExpanded

(it will be something like "if facebookRowsExpanded return 7, else return 5")

NOTE: your code for cellForRowAtIndexPath must use facebookRowsExpanded.

(it should return the correct string, depending on whether it expands or not.)

+11


source







All Articles