Why the need to create a new array?


#1

It seems a bit odd that you would need to create a whole new array to delete one item?

[code]//When the delete button is pressed this delegate method is called and must be implemented in the sub class to handle

  • (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
    {
    //If the table view is asking to commit a delete command…
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
    //We remove the row being deleted from the possessions array
    [possessions removeObjectAtIndex:[indexPath row]];

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

    }
    }[/code]


#2

True, it does. But this method has dual use: it can delete a set of rows or just a single row. For our use, we only need to delete one row. You could see how other applications might delete more than row at a time.

If it bugs you, you could always create a category on UITableView to pass a single NSIndexPath to delete… :slight_smile: Now that’s a challenge!


#3

I think I will wait until I have finished the book first :slight_smile:


#4

Hi,

Joe - it is a testament to your book that I would even attempt such a challenge.

Anyway here goes - please feel free to rip it apart.

//
//  TestCategory.h
//

#import <Foundation/Foundation.h>

@interface UITableView (DeleteOneRow)

- (void)deleteRowAtIndexPath:(NSIndexPath *)indexPath withRowAnimation:(UITableViewRowAnimation)animation;

@end
//
//  TestCategory.m
//
#import "TestCategory.h"

@implementation UITableView (DeleteOneRow)

- (void)deleteRowAtIndexPath:(NSIndexPath *)indexPath withRowAnimation:(UITableViewRowAnimation)animation

{
    [self deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
					 withRowAnimation:animation];
}

@end
//
//  ItemsViewController.m
//
...
#import "TestCategory.h"
...

-(void)tableView:(UITableView *) tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
	if (editingStyle == UITableViewCellEditingStyleDelete)
	{
		[possessions removeObjectAtIndex:[indexPath row]];
		[tableView deleteRowAtIndexPath:indexPath
						 withRowAnimation:UITableViewRowAnimationFade];
...

Is that what you meant ?

Gareth


#5

Yup, that’s exactly what I meant.


#6

Well all credit to you and Aaron - I wouldn’t have understood the question before reading your book.

Even topics that you haven’t covered seem so much easier to grasp now.

My sincere thanks.

Gareth