tableView developed by iOS realizes left slip deletion function

  • 2021-10-13 08:50:58
  • OfStack

Preface

These days to achieve the left deletion function, found that a lot of online posts are mostly from the hands of one person, and then are copy articles, in fact, are not so complex, only to achieve a proxy method can be

The method is as follows


- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
 if (editingStyle == UITableViewCellEditingStyleDelete) {

 //  Delete data from a data source ,self.cellData It's your own data 
 [self.cellData removeObjectAtIndex:indexPath.row];
 //  Delete data from a list 
 [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
 }

}

The text deleted by default is Delete, which should be implemented in Chinese instead


- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
 return @" Delete ";// The default text is  Delete
}

The following two proxy methods can be written without writing, which is the default


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

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
 return YES;
}

If you report this error:


'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (5) must be equal to the number of rows contained in that section before the update (5), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out)

You have confused the order of these two methods in the proxy method, deleting the data first, and then deleting cell

[self.cellData removeObjectAtIndex:indexPath.row]; This method comes first

[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; This method is later

Also, don't go until you don't set up an agent. tableView.delegate = self;

Summarize

The above is all about iOS using tableView to realize the left deletion function. I hope the content of this article can help iOS developers. If you have any questions, you can leave a message for communication.


Related articles: