Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to show delete button on UITableView on swipe left ?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 845
    Comment on it

    One of the commonly used feature in UITableView is to show delete button on the swipe of the table view cell and perform the delete operation. Let us take an example to understand and implement this :-

    Begin by creating a new project and assign a name to it. Then in the Main.Storyboard in the ViewController drag a UITableView and UITableViewCell from the properties windows. In the attributes inspector of UITableViewCell assign the identifier of the cell.

    Now in the ViewController.m implement the following code :-

    @interface ViewController ()
    {
        NSMutableArray *arrOfNames;
    }
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
       
        arrOfNames = [[NSMutableArray alloc]initWithObjects:@"Objective C",@"C++",@"Java",@"Sql", nil];
        // Do any additional setup after loading the view, typically from a nib.
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
        
        return arrOfNames.count;      // returns the number of elements in the array
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        
        NSString *cellIdentifier = @"cell";  // Identifier of the cell given in the Main.Storyboard
      
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
        [cell.textLabel setText:[arrOfNames objectAtIndex:indexPath.row]];// set the names in the label
        
        return cell;
    }
    
                           /*  Method in which the delete operation is performed */
    
    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
    {
         if (editingStyle == UITableViewCellEditingStyleDelete) {
            
                [arrOfNames removeObjectAtIndex:indexPath.row];
                
                [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];    // it will show the deletion with animation
                [tableView reloadData];
                
            }
            
        }
    
    
    - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
        return YES;     // return YES if we want some specified item to be editable
    }
    
    @end
    

    Its done !

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: