Monday, November 15, 2010

Delete and add row from Table View



In this tutorial will learn how to delete and also how to add row into the table view, with the help of edit button on navigation bar.
Delete and add row from Table View
In this tutorial will learn how to delete and also how to add row into the table view, with the help of edit button on navigation bar. When we press Edit button it will show you ADD and Delete buttons on Table view.Table views are commonly found in iPhone applications, especially productivity applications. They are versatile user-interface objects that you can adapt for different needs. A table presents a scrollable list of items (or rows) that may be divided into sections. Each row may display strings, images, or other identifiers of the represented data item.
Table View Programming Guide for iPhone OS explains the concepts underlying table-view programming and shows how to create and manage table views in your own projects. It starts by giving an overview of table styles and characteristics, as well as the programmatic interfaces related to table views. It also explains in general terms how to map collections of data at various levels of a hierarchical data model to sequences of table views, and how to use certain UIKit classes to support this traversal of a data hierarchy. After giving the conceptual background, the guide discusses various aspects of table view programming, such as creating and configuring table views.

Final view will look like this:


My project name is DeleteRow and is Based on View Based Application, will add code to view controller file and then make connection into the Interface Builder.

Project created is View Based Application so we have to declare the Table view and to insert object into the Table View, we need to declare Array into DeleteRowViewController.h and also the action for the Edit function, Edit button is present on the Navigation bar and Navigation bar is add into the DeleteRowAppDelegate.m.

DeleteRowViewController.h file will look like this:
#import <UIKit/UIKit.h>
@interface DeleteRowViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
{
    IBOutlet UITableView *Table;
    NSMutableArray *arry;
}
- (IBAction) EditTable:(id)sender;
@end
After declaring the variable for the table and the action for the Edit button which is present on Navigation bar. Will add this to insert object into the Table View and setting title for the table and also the Button on Bar button is added with the title, this is written in viewDidLoad method.
Now will set number of section in table view and also the number of rows into the section, this is done by counting the array and in that will set condition for editing with if function.
After setting the number of section and rows into section in table view, will set the tablecell into table view and then if editing and the index path is not zero then the count moves to one and then will start editing.
Now the Action for Add Button and the Delete Button is declared and in that will will write method for that action. In AddButtonAction will add an object into the Table View with the array we used earlier using reloadData method and in DeleteButtonAction will delete object from Table by removeLastObject and is then reloaded the data into the Table view so the deleted object is removed.
- (IBAction)AddButtonAction:(id)sender
{
    [arry addObject:@"Tutorial "];
    [Table reloadData];
}
- (IBAction)DeleteButtonAction:(id)sender
{
    [arry removeLastObject];
    [Table reloadData];
}
In EditTable Action we are setting the method for it. If IF condition is satisfied then the title for the button is Edit and is set on the navigation bar that is on left side of the navigation Bar and if not satisfied then the title for the button is changed and written as Done.
- (IBAction) EditTable:(id)sender
{
    if(self.editing)
    {
      [super setEditing:NO animated:NO];
      [Table setEditing:NO animated:NO];
      [Table reloadData];
        [self.navigationItem.leftBarButtonItem setTitle:@"Edit"];
        [self.navigationItem.leftBarButtonItem setStyle:UIBarButtonItemStylePlain];
    }
    else
    {
      [super setEditing:YES animated:YES];
      [Table setEditing:YES animated:YES];
      [Table reloadData];
        [self.navigationItem.leftBarButtonItem setTitle:@"Done"];
        [self.navigationItem.leftBarButtonItem setStyle:UIBarButtonItemStyleDone];
    }
}
In this method the row is deleted and added if IF condition is satisfied then the object on table view which is last will be deleted, if another IF condition is satisfied then the object is add into the table View with object name as Tutorial.
- (void)tableView:(UITableView *)aTableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        [arry removeObjectAtIndex:indexPath.row];
      [Table reloadData];
    else if (editingStyle == UITableViewCellEditingStyleInsert)
    {
        [arry insertObject:@"Tutorial" atIndex:[arry count]];
      [Table reloadData];
    }
}
DeleteRowViewController.m file will look like this:
#import "DeleteRowViewController.h"
@implementation DeleteRowViewController
- (void)viewDidLoad
{
    arry = [[NSMutableArray allocinitWithObjects:@"Rose",@"India",@"Rose India",@"Technology",@"Tutorial",nil];
    self.title = @"Edit Table ";
    UIBarButtonItem *addButton = [[UIBarButtonItem allocinitWithTitle:@"Edit"style:UIBarButtonItemStyleBordered target:self action:@selector(EditTable:)];
    [self.navigationItem setLeftBarButtonItem:addButton];
    [super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
}
- (void)dealloc
{
    [super dealloc];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    int count = [arry 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 allocinitWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }
    int count = 0;
    if(self.editing && indexPath.row != 0)
      count = 1;
    NSLog([NSString stringWithFormat:@"%i,%i",indexPath.row,(indexPath.row-count)]);
     if(indexPath.row == ([arry count]) && self.editing)
    {
        cell.textLabel.text = @"ADD";
      return cell;
    }
    cell.textLabel.text = [arry objectAtIndex:indexPath.row];
    return cell;
}
- (IBAction)AddButtonAction:(id)sender
{
    [arry addObject:@"Tutorial "];
    [Table reloadData];
}
- (IBAction)DeleteButtonAction:(id)sender
{
    [arry removeLastObject];
    [Table reloadData];
}
- (IBAction) EditTable:(id)sender
{
    if(self.editing)
    {
      [super setEditing:NO animated:NO];
      [Table setEditing:NO animated:NO];
      [Table reloadData];
        [self.navigationItem.leftBarButtonItem setTitle:@"Edit"];
        [self.navigationItem.leftBarButtonItem setStyle:UIBarButtonItemStylePlain];
    }
    else
    {
      [super setEditing:YES animated:YES];
      [Table setEditing:YES animated:YES];
      [Table 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 == ([arry count]))
    {
      return UITableViewCellEditingStyleInsert;
    else
    {
      return UITableViewCellEditingStyleDelete;
    }
    return UITableViewCellEditingStyleNone;
}
- (void)tableView:(UITableView *)aTableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
   if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        [arry removeObjectAtIndex:indexPath.row];
      [Table reloadData];
    else if (editingStyle == UITableViewCellEditingStyleInsert)
    {
        [arry insertObject:@"Tutorial" atIndex:[arry count]];
      [Table reloadData];
    }
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath
      toIndexPath:(NSIndexPath *)toIndexPath
{
    NSString *item = [[arry objectAtIndex:fromIndexPath.rowretain];
    [arry removeObject:item];
    [arry insertObject:item atIndex:toIndexPath.row];
    [item release];
}
@end
Make connection as shown
:
Finally Press Build And Go

1 comment:

  1. [arry insertObject:@"Tutorial" atIndex:[arry count]];

    should be written as

    [arry addObject:@"Tutorial"];

    ReplyDelete

Share it

Sign In

Followers