In this application we will see how to content delete , edit from the Table View. So let see how it will worked.

Step 1: Open the Xcode, Create a new project using View Base application. Give the application “TableView”.

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: Expand classes and notice Interface Builder created the FirstViewController and SecondViewController
class for you. Expand Resources and notice the template generated a separate nib, TableViewViewController.xib for the TableView application.

Step 4: Open the TableViewAppDelegate.m file make the following changes:

#import "TableViewAppDelegate.h"
#import "TableViewViewController.h"
@implementation TableViewAppDelegate
@synthesize window=_window;
@synthesize viewController=_viewController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)
launchOptions
{
UINavigationController *navController = [[UINavigationController alloc]
initWithRootViewController:_viewController];
[self.window addSubview:navController.view];
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application
{
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{}
- (void)applicationWillTerminate:(UIApplication *)application
{
}
- (void)dealloc
{
[_window release];
[_viewController release];
[super dealloc];
}
@end

Step 5: Open the TableViewViewController.h file make the following changes:

#import <UIKit/UIKit.h>
@interface TableViewViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
{
IBOutlet UITableView *tableView1;
NSMutableArray *arry;
}
- (IBAction) EditTable:(id)sender;
@end

Step 6: Double click the TableViewViewController.xib file and open it to the Interface Builder. First drag the table view from the library and place it to the view window.Select the Table View from the view and bring up Connection Inspector and connect datasource to File’s owner icon and delegate to the File’s owner icon. Now save the .xib file, close it and go back to the Xcode.

Step 7: Open the TableViewViewController.m file make the following changes:

#import "TableViewViewController.h"
@implementation TableViewViewController
- (void)viewDidLoad
{
arry = [[NSMutableArray alloc]
initWithObjects:@"iPhone",@"MacMini",@"iMac",@"MacBookProAir",@"MacBookPro",nil];
self.title = @"Table View ";
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithTitle:@"Edit"
style:UIBarButtonItemStyleBordered target:self action:@selector(EditTable:)];
[self.navigationItem setLeftBarButtonItem:addButton];
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (void)dealloc
{
[super dealloc];
}
#pragma mark Table view methods
- (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 alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
int count = 0;
if(self.editing && indexPath.row != 0)
count = 1;
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:@"MacBookPro "];
[tableView1 reloadData];
}
- (IBAction)DeleteButtonAction:(id)sender
{
[arry removeLastObject];
[tableView1 reloadData];
}
- (IBAction) EditTable:(id)sender
{
if(self.editing)
{
[super setEditing:NO animated:NO];
[tableView1 setEditing:NO animated:NO];
[tableView1 reloadData];
[self.navigationItem.leftBarButtonItem setTitle:@"Edit"];
[self.navigationItem.leftBarButtonItem setStyle:UIBarButtonItemStylePlain];
}
else
{
[super setEditing:YES animated:YES];
[tableView1 setEditing:YES animated:YES];
[tableView1 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];
[tableView1 reloadData];
} else if (editingStyle == UITableViewCellEditingStyleInsert)
{
[arry insertObject:@"MacBookPro" atIndex:[arry count]];
[tableView1 reloadData];
}
}
#pragma mark Row reordering
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
@end

Step 8: Now Compile and run the application on the Simulator.

You can Download SourceCode from here

Did you enjoy this article?
Share
the
Love
Get Free Updates