Add Delete Table Cell in iPhone

Introduction

In this article I will create a Single View application using a table view from xib. When we click on a table cell we show images in the grid form.

To understand it we use the following.

Step 1

Open XCode by double-clicking on it.

Step 2

Create a New XCode Project by clicking on it.

Step 3

Now select Single View Application and click on Next.

Step 4

Now provide your Product Name and Company Identifier.

Step 5

Select the location where you want to save your project and click on Create.

Step 6

Now here we write the code.

ViewController.h

#import <UIKit/UIKit.h>
@interface ViewController : UIViewController <UITableViewDelegate,UITableViewDataSource>
{
IBOutlet UITableView *tableView;
NSMutableArray *dataList;
}
- (IBAction) Edit:(id)sender;
@end

ViewController.m

#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
dataList = [[NSMutableArray alloc] initWithObjects:@"SunDay",@"MonDay",@"TuesDay",@"WednesDay",@"ThusDay",@"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.textLabel.text = @"Add Data";
return cell;
}
cell.textLabel.text = [dataList objectAtIndex:indexPath.row];
return cell;
}
- (IBAction)AddButtonAction:(id)sender{
[dataList addObject:@"SunDay"];
[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 && 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:@"Sunday" atIndex:[dataList count]];
[tableView reloadData];
}
}
#pragma mark Row reordering
- (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];
}
@end

Step 7

Finally we click on the Run button to show the output.

Step 8

Output1 in iPhone:

Output1-in-iPhone.png

Output2 in iPhone:

Output2-in-iPhone.png

Output3 in iPhone:

Output3-in-iPhone.png

Output4 in iPhone:

Output4-in-iPhone.png

Output5 in iPhone:

Output5-in-iPhone.png

Up Next
    Ebook Download
    View all
    Learn
    View all