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.

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 &amp;&amp; indexPath.row != 0)
  count = 1;

  if(indexPath.row == ([dataList count]) &amp;&amp; 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{
  if(self.editing)
 {
   [super setEditing:NO animated:NO];
   [tableView setEditing:NO animated:NO];
   [tableView reloadData];
   [self.navigationItem.leftBarButtonItem setTitle:@"Edit"];
   [self.navigationItem.leftBarButtonItem setStyle:UIBarButtonItemStylePlain];
 }
else
{
   [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 &amp;&amp; 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

WP Greet Box icon
Hello there! If you are new here, you might want to subscribe to the RSS feed for updates on this topic, and free programming tips and tricks and source code            snippets.

2 Responses to “How to Add Delete Data from TableView in iPhone”

  1. ADog says:

    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.

  2. AD says:

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

Leave a Reply

Security Code:

learn iphone programming

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Our Mobile Training Courses

EDUmobile.ORG offers the following 4 Mobile Training Courses. Our iPhone Training Course is very popular, with over 200 developers in training.

learn iphone programming
© 2010 EDUmobile.ORG