SoFunction
Updated on 2025-04-12

UITableView left swipe customization option and batch deletion implementation in iOS applications

Implement the customization option of left swiping of UITableView
When the UITableView enters editing mode, the Delete button will appear on the right side of the cell that performs the left swipe operation. How to customize the button that appears on the left swipe?

You only need to implement this proxy method below UITableView.

Copy the codeThe code is as follows:

- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewRowAction *likeAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"Like" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
// Implement relevant logic code
      // ...
// In the end, I hope the cell can automatically return to the default state, so I need to exit the editing mode
      = NO;
    }];

UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Delete" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
// Change the model first
      [ removeObjectAtIndex:];
// Then refresh the view
      [ deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
// There is no need to actively exit the editing mode. After the operation of updating the view above is completed, it will automatically exit the editing mode.
    }];

    return @[deleteAction, likeAction];
}


At this time, two buttons will appear, one is like and the other is delete. The order of occurrence is related to the order of elements in the array returned in this method.

If the above method is implemented, the tableView:commitEditingStyle:forRowAtIndexPath: and tableView: titleForDeleteConfirmationButtonForRowAtIndexPath: methods mentioned earlier will not be called again. (If you want to be compatible with previous versions, you need to implement the tableView:commitEditingStyle:forRowAtIndexPath: method, and you need to do nothing in this method.)


Multiple lines of UITableview are deleted at the same time
The following code is used with xib, but the key is not here. Remember the delegation used later.

The essence is the deletion operation of array array.

Copy the codeThe code is as follows:

//
// 
//  UITableViewDelteMutilRows
//

#import ""

@implementation UITableViewDelteMutilRowsViewController
@synthesize tableview;
@synthesize dataArray;
@synthesize deleteDic;
@synthesize leftButton;
@synthesize rightButton;
#pragma mark -
#pragma mark View lifecycle


- (void)viewDidLoad {
    [super viewDidLoad];
    dataArray = [[NSMutableArray alloc] initWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",nil];
    deleteDic = [[NSMutableDictionary alloc] init];

= @"Edit";
}

- (IBAction)choseData{
if ( == @"Edit") {
= @"OK";
        [ setEditing:YES animated:YES];
    }
    else {
= @"Edit";
        [deleteDic removeAllObjects];
        [ setEditing:NO animated:YES];
    }

}
- (IBAction)deleteFuntion{
    [dataArray removeObjectsInArray:[deleteDic allKeys]];
   
    [ deleteRowsAtIndexPaths:[NSArray arrayWithArray:[deleteDic allValues]] withRowAnimation:UITableViewRowAnimationFade];
    [deleteDic removeAllObjects];
   
}

- (void)dealloc {
    [leftButton release];
    [rightButton release];
    [deleteDic release];
    [dataArray release];
    [tableview release];
    [super dealloc];
}
#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return [dataArray count];
}


// Customize the appearance of table view cells.
- (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...
    = [dataArray objectAtIndex:];
    return cell;
}


/*// This is set to be swiped to edit and delete
 // Override to support conditional editing of the table view.
 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
 // Return NO if you do not want the specified item to be editable.
 return YES;
 }
 */

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
}

#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (== @"OK") {
        [deleteDic setObject:indexPath forKey:[dataArray objectAtIndex:]];  
    }
    else {
    }
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
if ( == @"OK") {
        [deleteDic removeObjectForKey:[dataArray objectAtIndex:]];
    }
}
@end