3

I am having problems with deleting rows from table view. I am using the code below when the delete button in the row was pressed:

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];

First row was deleted successfully but then, indexes were not correct. So when I delete the last row, it gives index out of bounds exception.

The cell generation code is:

- (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 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.

Burak
  • 5,706
  • 20
  • 70
  • 110

1 Answers1

11

Assuming your [NSIndexPath indexPathForRow:control.tag-100 inSection:0]; returns the right index path.... Did you try moving the removeObjectAtIndex call inside the begin-/end-updates "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 your cell.rateView.tag become wrong, after youe delete your first cell. So after every deletion (i.e. every removeObjectAtIndex...) you must re-iterate over your remaining tableview-cells an re-assign the proper tag-value (cell.rateView.tag = indexPath.row + 100)! Otherwise your [NSIndexPath indexPathForRow:control.tag-100 inSection:0]; will return a wrong indexPath, therefore leading to your out of bounds error!

Re-assigning the tag-values:

You don't have to reload the entire table, just loop through the remaining cells an re-assign 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 do:

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

(Or do the re-assignment even in the inner-loop of the first code-snippet directly.)


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

Note that facebookRowsExpanded is a class variable 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 has to return the correct row, depending on whether or not you are expanded.)

Fattie
  • 27,874
  • 70
  • 431
  • 719
Nenad M
  • 3,055
  • 20
  • 26
  • Everything worked fine until the last row. When it comes to last row, it stucks again. I guess, after deleting row, tag is not re-assigned – Burak Feb 07 '13 at 10:42
  • Yes, therefore I should reload table. If I reload, animation does not happen – Burak Feb 07 '13 at 10:49