How To Add Delete Data From TableView In iPhone

In this application we will see how to add delete data from the tableview. Let see how it will worked.

Step 1: Create a new application using View base template. Give the application name “AddDelete”.

Step 2: Xcode automatically creates the directory structure and adds essential frameworks to it. You can explore the directory structure to check out the content of the directory.

Step 3: xpand classes and notice Interface Builder created the AddDeleteViewController class for you. Expand Resources and notice the template generated a separate nib, AddDeleteViewController.xib, for the “AddDelete”.

Step 4: In the AddDeleteAppDelegate.m file make the following changes:


- (void)applicationDidFinishLaunching:(UIApplication *)application {
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
[window addSubview:navigationController.view];
[window makeKeyAndVisible];



Step 5: Open the AddDeleteViewController.h file, in this file we have added two protocols that are needed for it to act as the delegate and datasource for the table view,declare UITableView for displaying the list of data and NSMutableArray for insertion and deletion operations to the basic array handling and added one IBAction: method.


#import <uikit/UIKit.h>

@interface AddDeleteViewController : UIViewController <uitableViewDelegate,UITableViewDataSource>{

IBOutlet UITableView *tableView;
NSMutableArray *dataList;

- (IBAction) Edit:(id)sender;



Step 6: Double click your AddDeleteViewController.xib file and open it to the Interface Builder. First drag the tableview from the library and place it to the view window, select the tableview from the view window and bring up connection Inspector, then you’ll notice that the first two available connections fare dataSource and delegate. Drag from the circle next to each of those connections over the File’s Owner icon. Now select the File’s Owner icon from the main window and bring up Connection Inspector and drag from the tableView to TableView. You have done all the connection, so save the .xib file, close it and go back to the Xcode.

Step 7: In the AddDeleteViewController.m file make the following changes :


- (void)viewDidLoad {
dataList = [[NSMutableArray alloc] initWithObjects:@"SunDay",@"MonDay",@"TuesDay",@"WednesDay",@"Thus Day",@"FriDay",@"SaturDay",nil];
self.title = @"Add Delete Data Example";
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithTitle:@"Edit" style:UIBarButtonItemStyleBordered target:self action:@selector(Edit:)];
[self.navigationItem setLeftBarButtonItem:addButton];
[super viewDidLoad];

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
int count = [dataList count];
if(self.editing) count++;
return count;

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
cell.hidesAccessoryWhenEditing = YES;
int count = 0;
if(self.editing && indexPath.row != 0)
count = 1;

if(indexPath.row == ([dataList count]) && self.editing){
cell.text = @"Add Data";
return cell;

cell.text = [dataList objectAtIndex:indexPath.row];
return cell;

- (IBAction)AddButtonAction:(id)sender{
[dataList addObject:@"SaturDay"];
[tableView reloadData];

- (IBAction)DeleteButtonAction:(id)sender{
[dataList removeLastObject];
[tableView reloadData];

- (IBAction) Edit:(id)sender{
[super setEditing:NO animated:NO];
[tableView setEditing:NO animated:NO];
[tableView reloadData];
[self.navigationItem.leftBarButtonItem setTitle:@"Edit"];
[self.navigationItem.leftBarButtonItem setStyle:UIBarButtonItemStylePlain];
[super setEditing:YES animated:YES];
[tableView setEditing:YES animated:YES];
[tableView reloadData];
[self.navigationItem.leftBarButtonItem setTitle:@"Done"];
[self.navigationItem.leftBarButtonItem setStyle:UIBarButtonItemStyleDone];

- (UITableViewCellEditingStyle)tableView:(UITableView *)aTableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
if (self.editing == NO || !indexPath) return UITableViewCellEditingStyleNone;
if (self.editing && indexPath.row == ([dataList count])) {
return UITableViewCellEditingStyleInsert;
} else {
return UITableViewCellEditingStyleDelete;
return UITableViewCellEditingStyleNone;

- (void)tableView:(UITableView *)aTableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath {

if (editingStyle == UITableViewCellEditingStyleDelete) {
[dataList removeObjectAtIndex:indexPath.row];
[tableView reloadData];
} else if (editingStyle == UITableViewCellEditingStyleInsert) {
[dataList insertObject:@"SaturDay" atIndex:[dataList count]];
[tableView reloadData];

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath
toIndexPath:(NSIndexPath *)toIndexPath {
NSString *item = [[dataList objectAtIndex:fromIndexPath.row] retain];
[dataList removeObject:item];
[dataList insertObject:item atIndex:toIndexPath.row];
[item release];



Step 8: Now compile and run the application in the Simulator.

You can Downloaded SourceCode from here AddDelete 2

Leave a Comment:

ADog says June 22, 2010

Downloaded source code. Tried it out Add Data adds SturDay… no complaints there, but when I tried moving several rows to different places, the program terminated due to an uncaught exception. This code is not very reliable. Please fix. Thanks for the tutorial attempt by the way.

AD says July 15, 2010

Crashes when you try to move the “Add entry” button

caspa says August 3, 2011

this disables the option to move the add entry button
– (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row != [dataList count])
return YES;
return NO;

Jose says May 11, 2012

– Not up to date
– Code comes with a few errors
– Source code download is for Mac OSX 64 bit

Add Your Reply