Delete a Row from UITableView and Model-View-Controller
As said in the previous post, I have another question to answer before moving onto Storyboard tutorial.
How can I delete a row from UITableView?
This is another common question people raised when building the Simple Table App. Again, it’s easier than you thought. But before jumping into the coding part, I have to introduce you the Model-View-Controller model, which is one of the most quoted design patterns for user interface programming.
You can’t escape from learning Model-View-Controller (MVC for short) if you’re serious about iOS programming. Not limited to iOS programming, MVC is commonly used and quoted in other programming languages such as Java. If you come from other programming backgrounds, MVC shouldn’t be new to you.

Understanding Model-View-Controller
At the heart of MVC, and the idea that was the most influential to later frameworks, is what I call Separated Presentation. The idea behind Separated Presentation is to make a clear division between domain objects that model our perception of the real world, and presentation objects that are the GUI elements we see on the screen. Domain objects should be completely self contained and work without reference to the presentation, they should also be able to support multiple presentations, possibly simultaneously. This approach was also an important part of the Unix culture, and continues today allowing many applications to be manipulated through both a graphical and command-line interface.
No matter what computer language you learn, one important concept that makes you become a better programmer is Separation of Concerns (SoC). The concept is pretty simple. Concerns are the different aspects of software functionality. The concept encourages developers to break a big feature or program into several areas of concern that each area has its own responsibility. The delegate pattern that is commonly found in iOS programming we explained in the earlier tutorial is one of the example of SoC.
Here, model-view-controller is another example of SoC. The core idea behind MVC is to clearly separate user interface into three areas (or groups of objects) that each area is responsible for a particular functionality. As the name suggests, MVC breaks an user interface into three parts:
Model – model is responsible for holding the data or any operations on the data. The model can be as simple as an array object that stores all the table data. Add, edit and delete are examples of the operations. In reality, the operations are usually known as business rules.
View – view manages the visual display of information. For example, UITableView shows information in a table view format.
Controller – controller is the bridge between model and view. It translates the user interaction from the view (e.g. tap) into appropriate action to be performed in the model. For example, user taps a delete button in the view and controller, in turn, triggers a delete operation in the model. After that, it also requests the view to refresh itself to reflect the update of the data model.
To better help you understand MVC, let’s use our Simple Table app as an example. The app displays a list of recipes in the table view. If you turn the concept into visual representation, here is how the table data is displayed:

MVC Model Illustrated Using Simple Table as Example
The recipe information that are stored in separate array objects is the Model. Each table row maps to an element of the recipe arrays. The UITableView object is the View that is the interface to be seen by the user. It’s responsible for all the visuals (e.g. color of the table rows, font size and type). The TableViewController acts as the bridge between the TableView and Recipe data model. When display table data, UITableView actually asks the Controller for the data to display, that in turn, picks the data from the model.
How To Delete a Row from UITableView
I hope you have a better understanding about Model-View-Controller. Now let’s move onto the coding part and see how we can delete a row from UITableView. To make thing simple, I’ll use the plain version of Simple Table app as an example.
If you thoroughly understand the MVC model, you probably have some ideas how to implement row deletion. There are three main things we need to do:
1. Write code to switch to edit mode for row deletion
2. Delete the corresponding table data from the model
3. Reload the table view in order to reflect the change of table data
1. Write code to switch to edit mode for row deletion
In iOS app, user normally swipes across a row to initiate the delete button. Recalled that we have adopted the UITableViewDataSource protocol, if you refer to the API doc, there is a method named tableView:commitEditingStyle:forRowAtIndexPath. When user swipes across a row, the table view will check to see if the method has been implemented. If the method is found, the table view will automatically show the “Delete” button.
Simply add the following code to your table view app and run your app:
1 2 3 4 |
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { } |
Even the method is empty and doesn’t perform anything, you’ll see the “Delete” button when you swipe across a row.

Swipe to Delete a Table Row
2. Delete the corresponding table data from the model
The next thing is to add code to the method and remove the actual table data. Like other table view methods, it passes the indexPath as parameter that tells you the row number for the deletion. So you can make use of this information and remove the corresponding element from the data array.
In the original code of Simple Table App, we use NSArray to store the table data (which is the model). The problem of NSArray is it’s non-editable. That is, you can’t add/remove its content once the array is initialized. Alternatively, we’ll change the NSArray to NSMutableArray, which adds insertion and deletion operations:
1 2 3 4 5 6 7 8 9 10 11 |
@implementation SimpleTableViewController { NSMutableArray *tableData; } - (void)viewDidLoad { [super viewDidLoad]; // Initialize table data tableData = [NSMutableArray arrayWithObjects:@"Egg Benedict", @"Mushroom Risotto", @"Full Breakfast", @"Hamburger", @"Ham and Egg Sandwich", @"Creme Brelee", @"White Chocolate Donut", @"Starbucks Coffee", @"Vegetable Curry", @"Instant Noodle with Egg", @"Noodle with BBQ Pork", @"Japanese Noodle with Pork", @"Green Tea", @"Thai Shrimp Cake", @"Angry Birds Cake", @"Ham and Cheese Panini", nil]; } |
In the tableView:commitEditingStyle method, add the following code to remove the actual data from the array. Your method should look like this:
1 2 3 4 5 |
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { // Remove the row from data model [tableData removeObjectAtIndex:indexPath.row]; } |
The NSMutableArray provides a number of operations for you to manipulate the content of an array. Here we utilize the “removeObjectAtIndex” method to remove a particular item from the array. You can try to run the app and delete a row. Oops! The app doesn’t work as expected.
It’s not a bug. The app does delete the item from the array. The reason why the deleted item still appears is the view hasn’t been refreshed to reflect the update of the data model.
3. Reload the table view
Therefore, once the underlying data is removed, we need to invoke “reloadData” method to request the table View to refresh. Here is the updated code:
1 2 3 4 5 6 7 8 |
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { // Remove the row from data model [tableData removeObjectAtIndex:indexPath.row]; // Request table view to reload [tableView reloadData]; } |
Test Your App and Delete a Row
Try to run your app again and swipe to delete a row. You should be able to delete it.

Delete a Table Row in Simple Table App
As always, leave me comment to share your experience about the tutorial.
Update: You can now download the source code of the Xcode project.
Comments
June
AuthorNot really understand this 1…
where shud i add those codes in?
Simon Ng
AuthorThis is a follow-up for the Simple Table app. So you can add the code in SimpleTableViewController.m.
Anuj Jain
Authorit is not working for my code….. 🙁
Simon Ng
AuthorCould you show me the error?
Anuj Jain
AuthorThere is no error message, but it is not displaying the delete button. It is working same as it was working after the previous tutorial.
champcar2000
AuthorIn the simulator, to get the Delete button to show, click the mouse – hold – then swipe to the left.
Anuj Jain
Authorit works…..thanx…. 🙂
Vivek Pansara
Authorstill not getting delete button…
Xavier Alfeiran
AuthorGreat tutorials so much easy to learn with your guides.
Ivan
AuthorIt gives me an error when I use the objectForKey function of NSDictionary to load the data. Is there some other way to do it?
champcar2000
Author..
champcar2000
AuthorWhen I run the simulator and tap the Delete button on a row, it crashes the program.
It appears that this is because I’ve done lessons 1-7 so my app has already been changed to use a property list, but this lesson still expects the app to be using a hard-coded array for the data. Is this the case??
Thanks in advance.
Sunny
AuthorFor the delete button, is the text is fixed ?
or can I change the word of the button from “Delete” to something else?
because I want to make a button for user to redirect to another page
means that if they tap on the table cell, the application will bring them to detail page
but when they tap on the button, they will be redirected to another page.
Matthew Kwan
AuthorIf you have implemented the Property List in tutorial 6, then coming here to remove the row from the array, you will get an error. To fix the error you just have to change NSDictionary to NSMutableDictionary.
By the way all your tutorials are awesome, very informative and easy to follow. Good job buddy!
Rumen Dimitrov
AuthorThank you for pointing this out!
Bryan Lee
AuthorThanks you so much 😀
abraham
Authorthank you bro 🙂
Akshay Gangurde
Authorthanks for property list’s specification,,, 🙂
Vishwas Shashidhar
AuthorThank you mate 🙂 that helped…
chelsea blues
AuthorThanx a lot mate ..
Jhlx
Authorthanks!
le van pha
Authorinteresting^^
Thomas Moore
AuthorThat was pretty easy! Loving the tutorials. Great work Simon!
abbiya
Authoryou should write more dude.. great work. thanks
Pramodh Bettadapura
AuthorHi Simon, after creating custom cell i made the above changes with edit button, but on click of edit button the cell resizes but not the text entered can you please help!
Guest
AuthorYou might consider rewriting this tutorial to use the
deleteRowsAtIndexPaths:withRowAnimation:
method instead of reloadData.http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableView_Class/Reference/Reference.html
steveluscher
AuthorYou might consider rewriting this tutorial using the deleteRowsAtIndexPaths:withRowAnimation: method instead of reloadData.
http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableView_Class/Reference/Reference.html
Sergiy Tkachuk
AuthorThank for reference. It looks much better with animation. I have found good example how to use deleteRowsAtIndexPaths:withRowAnimation: at http://stackoverflow.com/q/4276804/13441
Mark Thien
AuthorI changed to NSMutableArray and still getting error:
Terminating app due to uncaught exception ‘NSInternalInconsistencyException’, reason: ‘-[__NSCFArray removeObjectAtIndex:]: mutating method sent to immutable object’
I am using property list file:
NSString *path = [[NSBundle mainBundle] pathForResource:@”recipies” ofType:@”plist”];
recipies = [dict objectForKey:@”recipyName”];
Klaus Brunckhorst
AuthorGreat tutorial. Thanx!
In order to work with the example from the previous chapter I used ‘mutableCopy’ to fill the Arrays from the plist:
[..]
@implementation HalloWorldViewController
{
NSMutableArray *tableData;
NSMutableArray *thumbnails;
NSMutableArray *prepTime;
}
– (void)viewDidLoad
{
[super viewDidLoad];
// Find out the path of recipes.plist
NSString *path = [[NSBundle mainBundle] pathForResource:@”recipes” ofType:@”plist”];
// Load the file content and read the data into arrays
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];
tableData = [[dict objectForKey:@”RecipeName”] mutableCopy];
thumbnails = [[dict objectForKey:@”Thumbnail”] mutableCopy];
prepTime = [[dict objectForKey:@”PrepTime”] mutableCopy];
}
[..]
– (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
// Remove the row from data model
[tableData removeObjectAtIndex:indexPath.row];
[thumbnails removeObjectAtIndex:indexPath.row];
[prepTime removeObjectAtIndex:indexPath.row];
// Request table view to reload
[tableView reloadData];
}
[..]
indra tulku
Authorvery helpful, thank you
paul
AuthorThanks Klaus!
Phil
AuthorI’m new to Obj-C and iOS programming, I’m sorry if these are silly questions.
When we define tableData, why we have to call method mutableCopy? I checked reference of instance method objectForKey, its return type is “id”.
1) Is the “id” type not mutable?
2) Does it mean we have to concern if mutable type is needed for every single object?
Jhlx
AuthorThank you!
vishal
Authorwhy mutablecopy?
Nidhi
Authorsuper awesome 🙂
Khalid Mehmood Awan
Author“Here, model-view-controller is another example of SoC. The core idea behind MVC is to clearly separate user interface into three areas (or groups of objects) that each area is responsible for a particular functionality. As the name suggests, MVC breaks an user interface into three parts:”
here you are saying “user interface” , why not say “application instead of “user interface” ?
Khalid Mehmood Awan
AuthorHi Simon, I am using macbook pro with a normal USB mouse. I find it very difficult to swipe for the purpose to delete a table row. Sometimes it works while most of the time it does not.
is there any way to do it easily ?
khalidmehmoodawan at gmail
Khalid Mehmood Awan
AuthorExcellent tutorials 🙂
Lucas Ramos
AuthorHey, everybody! Thanks Matthew for the update.
Now, if you’re following the tutorial, beyond changing the dictionary to Mutable Dictionary,
You have to add:
NSMutableArray *thumbnails;
NSMutableArray *prepTime;[thumbnails removeObjectAtIndex:indexPath.row];
And
[prepTime removeObjectAtIndex:indexPath.row];
[thumbnails removeObjectAtIndex:indexPath.row];
In order to it work.
刘雨辰
AuthorI can delete a row using the tutorial’s code, but it has no animation or aesthetic, how to fix it, thanks.
shailesh
AuthorSuperb tutorial 🙂
aamir khan
Authorit is deleted my tables cells data helpfull tutorials great work
Learner
Authordidn’t any delete button shows, please post the full .m file code pls pls pls
learner
AuthorIt cant delete last row
priyank
AuthorHi,first of all thanks for this “Great tutorial”.I’m new in ios development…can u plz explain how to install sqlite and insert and retrive data from sqlite.
kaushik kumar
Authori did the same to delete the cell but its showing an exception as……’-[__NSCFArray removeObjectAtIndex:]: mutating method sent to immutable object’…but i am using nsmutable array
Ankit
AuthorI write the code of delete button. But delete button is not showing.Plz help me.
wahiba
AuthorIn the simulator, to get the Delete button to show, click the mouse – hold – then swipe to the left.
Ben
AuthorGreat tutorials. Just a simple side note… Being French and a cook in my spare time, I really have to point this out : One does not say crême brelée but Crême Brûlée (which literraly means : burnt cream)
abDullah
AuthorExcellent tutorials …… keep it coming 😉
Chilly
AuthorGreat tutorial. For me the only thing missing is rearranging the cell assignments after a row is delete
Abdurrahman Mubeen Ali
AuthorNice & easy tutorial. How can I get the DELETE button on the left of the text?
Sharad Goyal
Authori did it but it is not showing “delete” button please help me in this program
Patel Kaushik
Authori make table view apps by custom cell ,i implement delete row from table but can’t delete
please give me help for delete table data which is created by custom cell …
sergtk
AuthorHow to update existing data when model is changed? It is easy to create Mapping Model in Xcode, specify mapping between properties. But is not clear what to do next with it and how to use it. Are there any tutorial on this subject here? Thanks
Developer
AuthorHow do I save what has been deleted? Example: User deletes an item and then closes the app. When the user reopens the app the item is fully deleted and does not just reappear. Thanks
Rubin Zubin
AuthorSuppose if i want to add items or row to this then ?
Kenmux De
Authorthe source code is ok? why cant i see the delete button, neither with my ipad nor the iphone simulator?
Paco
AuthorHi,
I had the same problem and it turned out that the table view was too big to fit in the screen. Try to half the width of the table view and see if you still can’t see the delete button. If your problem is effectively the same as mine then you have to play with the size restrictions on the main story board. Unfortunately I am not advanced enough to tell how to do it. Good luck 🙂
Rithik Dharma
Authorthat one was really good…!
Kritbovorn Taweeyossak
Authorwhy when I run app again , the data still have full
le van pha
Authorhow to remove check mark when selected row again? thank!
Toqeer Ahmad
AuthorThank You Sir! 🙂
It helps me a lot ..
Charles
AuthorI followed this steps but in the end after I press end the whole app freezes for a second and then restarts completely, to the point I got to open it again, with no changes whatsoever. Why is that happening?
Charles
AuthorNVM, after founding my mistake Ill go commit suicide in a corner.
Nij
Authorin commitEditingStyle method, is it necessary to write following lines?
[tableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic] ;
and yeah very easy to understand ur tutorials thanks to u,, m new to iOS developing.
sheikh306
Authorgreat to learn here (Y) keep going on