Delete and add row from Table View iPhone


 

Delete and add row from Table View iPhone

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.

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 iPhone

In this tutorial will learn how to delete and also how to add row into the table view iPhone, 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 table cell 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.

0

- (IBAction) EditTable:(id)sender

{

    if(self.editing)

1

    {

      [super setEditing:NO animated:NO];

      [Table setEditing:NO animated:NO];

2

      [Table reloadData];

        [self.navigationItem.leftBarButtonItem setTitle:@"Edit"];

        [self.navigationItem.leftBarButtonItem setStyle:UIBarButtonItemStylePlain];

3

    }

    else

    {

4

      [super setEditing:YES animated:YES];

      [Table setEditing:YES animated:YES];

      [Table reloadData];

5

        [self.navigationItem.leftBarButtonItem setTitle:@"Done"];

        [self.navigationItem.leftBarButtonItem setStyle:UIBarButtonItemStyleDone];

    }

6

}

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

7

forRowAtIndexPath:(NSIndexPath *)indexPath

{

    if (editingStyle == UITableViewCellEditingStyleDelete)

8

    {

        [arry removeObjectAtIndex:indexPath.row];

      [Table reloadData];

9

    } else if (editingStyle == UITableViewCellEditingStyleInsert)

    {

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

0

      [Table reloadData];

    }

}

1

DeleteRowViewController.m file will look like this:

#import "DeleteRowViewController.h"

2

@implementation DeleteRowViewController

- (void)viewDidLoad

{

3

    arry = [[NSMutableArray alloc] initWithObjects:@"Rose",@"India",@"Rose India",@"Technology",@"Tutorial",nil];

    self.title = @"Edit Table ";

    UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithTitle:@"Edit" style:UIBarButtonItemStyleBordered target:self action:@selector(EditTable:)];

4

    [self.navigationItem setLeftBarButtonItem:addButton];

    [super viewDidLoad];

}

5

- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview

6

}

- (void)dealloc

{

7

    [super dealloc];

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

8

{

    return 1;

}

9

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    int count = [arry count];

0

    if(self.editing) count++;

    return count;

}

1

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

{

    static NSString *CellIdentifier = @"Cell";

2

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil)

    {

3

        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];

    }

    int count = 0;

4

    if(self.editing && indexPath.row != 0)

      count = 1;

    NSLog([NSString stringWithFormat:@"%i,%i",indexPath.row,(indexPath.row-count)]);

5

     if(indexPath.row == ([arry count]) && self.editing)

    {

        cell.textLabel.text = @"ADD";

6

      return cell;

    }

    cell.textLabel.text = [arry objectAtIndex:indexPath.row];

7

    return cell;

}

- (IBAction)AddButtonAction:(id)sender

8

{

    [arry addObject:@"Tutorial "];

    [Table reloadData];

9

}

- (IBAction)DeleteButtonAction:(id)sender

{

0

    [arry removeLastObject];

    [Table reloadData];

}

1

- (IBAction) EditTable:(id)sender

{

    if(self.editing)

2

    {

      [super setEditing:NO animated:NO];

      [Table setEditing:NO animated:NO];

3

      [Table reloadData];

        [self.navigationItem.leftBarButtonItem setTitle:@"Edit"];

        [self.navigationItem.leftBarButtonItem setStyle:UIBarButtonItemStylePlain];

4

    }

    else

    {

5

      [super setEditing:YES animated:YES];

      [Table setEditing:YES animated:YES];

      [Table reloadData];

6

        [self.navigationItem.leftBarButtonItem setTitle:@"Done"];

        [self.navigationItem.leftBarButtonItem setStyle:UIBarButtonItemStyleDone];

    }

7

}

- (UITableViewCellEditingStyle)tableView:(UITableView *)aTableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

{

8

      if (self.editing == NO || !indexPath) return UITableViewCellEditingStyleNone;

      if (self.editing && indexPath.row == ([arry count]))

    {

9

      return UITableViewCellEditingStyleInsert;

    } else

    {

0

      return UITableViewCellEditingStyleDelete;

    }

    return UITableViewCellEditingStyleNone;

1

}

- (void)tableView:(UITableView *)aTableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle

forRowAtIndexPath:(NSIndexPath *)indexPath

2

{

   if (editingStyle == UITableViewCellEditingStyleDelete)

    {

3

        [arry removeObjectAtIndex:indexPath.row];

      [Table reloadData];

    } else if (editingStyle == UITableViewCellEditingStyleInsert)

4

    {

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

      [Table reloadData];

5

    }

}

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath

6

{

    return YES;

}

7

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath

      toIndexPath:(NSIndexPath *)toIndexPath

{

8

    NSString *item = [[arry objectAtIndex:fromIndexPath.row] retain];

    [arry removeObject:item];

    [arry insertObject:item atIndex:toIndexPath.row];

9

    [item release];

}

@end

0

Make connection as shown

:

Finally Press Build And Go

1

Download Here

 

Ads