Delete item in table view


#1

Hi, the book provides such implementation to delete row in table view:

[code]- (void)tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
// If the table view is asking to commit a delete command…
if (editingStyle == UITableViewCellEditingStyleDelete)
{
BNRItemStore *ps = [BNRItemStore sharedStore];
NSArray *items = [ps allItems];
BNRItem *p = [items objectAtIndex:[indexPath row]];
[ps removeItem:p];

    // We also remove that row from the table view with an animation
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                     withRowAnimation:UITableViewRowAnimationFade];
}

}[/code]

I implemented it this way:

[code]

  • (void)tableView:(UITableView *)tableView
    commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
    forRowAtIndexPath:(NSIndexPath *)indexPath
    {
    // If the table view is asking to commit a delete command…
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
    [[BNRItemStore sharedStore] removeNthItem:indexPath.row];

      // We also remove that row from the table view with an animation
      [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                       withRowAnimation:UITableViewRowAnimationFade];
    

    }
    }[/code]

where removeNthItem:indexPath is:

-(void) removeNthItem: (NSInteger) n
{
    [allItems removeObjectAtIndex:n];

}

Any comments, which approach is better, etc?