Delete rows from TableView

In most iOS apps, the user can use a swipe gesture to delete a row in a Table View. In this tutorial we will fill a Table View with some data and we will delete some rows.

Update: Oct 8, 2014. The rewritten version in Swift for iOS 8 and Xcode 6 is available here.

Open Xcode and create a new Single View Application. For product name, use DeleteItemTableViewDemo and then fill out the Organization Name, Company Identifier and Class Prefix fields with your customary values. Make sure only iPhone is selected in Devices, and that the Use Storyboards checkbox and Use Automatic Reference Counting checkbox are selected.

Go to the MainStoryboard file, delete the Existing View Controller and drag a new TableView Controller to the canvas. Select the TableView Controller and In the Editor menu choose "Embed in Navigation Controller". This will put a Navigation Controller around the TableView Controller.

Next, Add a New Objective-C class to the project, make it a subview of UITableViewController and name it NumberViewController. in NumberViewController.m add the following property in the @interface section.

@interface NumberViewController ()

@property (nonatomic, strong) NSMutableArray *numbers;


In viewDidLoad we fill the array with numbers.

- (void)viewDidLoad {     [super viewDidLoad];

self.numbers = [@[@"One",@"Two",@"Three",@"Four",@"Five"] mutableCopy]; }

When we added our NumberViewController class some methods are created from the TableViewController template. Next, we must finetune some of this methods. In numberOfSectionsInTableView we change the return line to one, since we only have one section.

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
// Return the number of sections.
return 1;

In the tableView:numberOfRowsInSection we will return the number of items in our array.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
// Return the number of rows in the section.
return [self.numbers count];

In tableView:cellForRowAtIndexPath we will iterate through our numbers array and insert them in our TableView. Change this method in

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

// Configure the cell...
cell.textLabel.text = self.numbers[indexPath.row];

return cell;

    // Configure the cell...     cell.textLabel.text = self.numbers[indexPath.row];

    return cell; }[/objc]

Go back to the MainStoryboard and select the Table View. Select the Identity Inspector and change the Class to NumberViewController.

Select the Empty Prototype Cell in the TableView and select the Attributes Inspector. Change the Table View Cell Identifier to Cell

Build and Run, The Table View is filled with the numbers.

To delete a row from our table view we must implement the tableView:commitEditingStyle:forRowAtIndexPath delegate method.

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
if (editingStyle == UITableViewCellEditingStyleDelete)
[self.numbers removeObjectAtIndex:indexPath.row];
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

First, we remove the item from our numbers array. Next, we get the indexPath of the deleted row and this row will be deleted with an animation effect.

Build and Run, and swipe on a row to delete this row.

You can download the source code of the DeleteItemTableViewDemo at the ioscreator repository on github.