Home > Tutorials Home > Ios > UITableview Cell Editing in iOS

UITableview Cell Editing in iOS

Published on October 17, 2019 8:21:45 PM

Button Example

We are an online tutoring company that has laid its foundation in utilizing Multiple Intelligence methodology to understand specific needs and learning styles of a student.UITableViewCellEditing does not contain data, but relies on a data source to obtain the details it needs to display itself, when it needs them.So that we patch a particular student to a tutor having a compatible teaching style. We have devised a smart online tutoring system to give you measurable results.Schools, cramped with students, fail to give your child the edge, the motivation and the right teacher guidance to help him find the true potential. We at make sure that your child gets the perfect learning environment to outperform.

Step 1 : Open the XCode and create a new Single View Based Application template. Give the application name "UITableViewCellEditing Example". Copy this Code to the ViewController.h

#import <UIKit/UIKit.h>

@interface RootViewController : UITableViewController
    NSMutableArray *dataArray;

@property (nonatomic, retain) UIImageView *customImage;


Step 2 : Copy the Following Code into your ViewController.m File.

#import "RootViewController.h"

@implementation RootViewController

@synthesize customImage;

- (void)viewDidLoad
    [super viewDidLoad];
	dataArray = [[NSMutableArray alloc] init];
	[dataArray addObject:@"Red"];
	[dataArray addObject:@"Green"];
	[dataArray addObject:@"Blue"];
	[dataArray addObject:@"Yellow"];
	[dataArray addObject:@"Black"];
	[dataArray addObject:@"White"];
	[dataArray addObject:@"Purple"];
	[dataArray addObject:@"Orange"];
	[dataArray addObject:@"Cyan"];
	[dataArray addObject:@"Magenta"];
	[dataArray addObject:@"Brown"];
	[dataArray addObject:@"Pink"];
	[dataArray addObject:@"Violet"];
	[dataArray addObject:@"Burgundy"];
    self.navigationItem.leftBarButtonItem = self.editButtonItem;
	UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addNewItem)];
    self.navigationItem.rightBarButtonItem = rightButton;

//Drag And Drop a new Button on left side in TOP Bar and get Reff of this Method.
- (void)addNewItem { [dataArray addObject:@"Teal"]; [self.tableView reloadData]; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [dataArray count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; } // Configure the cell CGRect imageFrame = CGRectMake(5, 5, 45, 45); self.customImage = [[[UIImageView alloc] initWithFrame:imageFrame] autorelease]; self.customImage.image = [UIImage imageNamed:@"colorWheel.png"]; [cell.contentView addSubview:self.customImage]; CGRect labelFrame = CGRectMake(65,15, 150, 25); UILabel *customLabel = [[[UILabel alloc] initWithFrame:labelFrame] autorelease]; customLabel.font = [UIFont boldSystemFontOfSize:20]; customLabel.text = [dataArray objectAtIndex:indexPath.row]; [cell.contentView addSubview:customLabel]; return cell; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath: (NSIndexPath *)indexPath { return 55; } - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath: (NSIndexPath *)indexPath { // Return NO if you do not want the specified item to be editable. return YES; } - (void)tableView:(UITableView *)tableView commitEditingStyle: (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath: (NSIndexPath *)indexPath { if (editingStyle == UITableViewCellEditingStyleDelete) { // Delete the row from the data source. [dataArray removeObjectAtIndex:indexPath.row]; [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; } else if (editingStyle == UITableViewCellEditingStyleInsert) { // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view. } } @end