Use Access Indexpath to delete multiple selected cells
Before deletion:
After deletion:
Analysis: How do I delete multiple cells? This requires the use of the UITableView proxy method, that is, the cell when the cell is selected, and we also define a mutable array to store the selected data for later deletion. This is the way to store Indexpath, because each cell is selected, it corresponds to a indexpath, and the selected cells are added to the Guide view, that is, check the tick, but also to determine whether the tick is repeated, based on this to display the cell tag. Finally, all of the tagged data is deleted in the original database, and the table is refreshed by emptying the array of stored data. The removal process involves sorting issues, which will be described in the following code.
1 #import "ViewController.h" 2 #define NUM 3 @interface viewcontroller () <uitableviewdatasource,uitableviewdeleg Ate> 4 @property (strong,nonatomic) Nsmutablearray *products; Original Product stock 5 @property (strong,nonatomic) Nsmutablearray *cellindexpaths; Store the selected cell 6 @property (weak, nonatomic) Iboutlet UITableView *tableview; 7-(Ibaction) deletebuttonclicked: (Uibarbuttonitem *) sender; 8 9 @end @implementation Viewcontroller-(void) viewdidload {[Super viewdidload]; 15//Initialization 16 Self.products = [Nsmutablearray arraywithcapacity:num]; Self.cellindexpaths = [Nsmutablearray arraywithcapacity:num]; for (int i=0; i<num; i++), {nsstring *product = [NSString stringwithformat:@ "product-%02d", I]; [Self.products addobject:product]; 22} 23 24//Set data source and agent Self.tableView.dataSource = self; Self.tableView.delegate = self; 27} 28 29//Delete all selected cells Indexpath 30//Description: Every time you delete, if you alwaysDelete from the back of the array, the result is not affected, but, if removed from the previous, then the elements in the array will move forward sequentially, when their indexpath is changed, the result will be a problem. At this point, you need to sort the selected elements. -(Ibaction) deletebuttonclicked: (Uibarbuttonitem *) Sender 32 {33//Sort operations on selected elements (in ascending order) [self.cellindexpaths Sortusingcomparator:^nscomparisonresult (ID obj1, id obj2) {nsindexpath *ip1 = (nsindexpath*) obj1; 36 Nsindexpath *ip2 = (nsindexpath*) obj2; The PNs if (Ip1.row = = Ip2.row) Max {Nsorderedsame W > Ip2.row) (nsordereddescending): 46 {47 return nsorderedascending; 48} 49 50}]; 51 52//1. Remove the product from the original data in the selected cell (backward delete) [Self.cellindexpaths enumerateobjectswithoptions:nsenumerationreverse u singblock:^ (id obj, Nsuinteger idx, BOOL *stop) {si [self.products removeobjectatindex: ((nsindexpath*) obj). row]; 55}]; 56 57//2. Clears the selected array of records Nsarray *tempArray = [Nsarray arrayWithArray:self.cellIndexPaths]; [Self.cellindexpaths removeallobjects]; 60 61//3. Perform partial refresh [Self.tableview Deleterowsatindexpaths:temparray Withrowanimation:uitableviewrowanimatio Nleft]; #pragma mark-tableview Data source method 66//Each section has the number of row Nsinteger-() TableView: (UITableView *) TableView Numbero Frowsinsection: (Nsinteger) section (Self.products.count); 70} 71//Set contents of each cell-(UITableViewCell *) tab Leview: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath 73 {74//1. According to the Reuseidentifier, first go to the object pool to find The reused Cell object is static nsstring *reuseidentifier = @ "Productcell"; UITableViewCell *cell = [Self.tableview dequeuereusablecellwithidentifier:reuseidentifier]; 77//2. If not found, create the Cell object yourself if (cell = = nil) + + = [[UITableViewCell alloc]initwithstyle:uitabl Eviewcellstyledefault Reuseidentifier:reuseidentifier]; 81} 82//3. Setting the contents of a Cell object Cell.textLabel.text =Self.products[indexpath.row]; if ([self.cellindexpaths Containsobject:indexpath])//When first selected, mark the {cell.accessorytype = UI Tableviewcellaccessorycheckmark; In the case of the "*" ()///re-select, unmark Cell.accessorytype = Uitableviewcellaccessorynone; 92} 93 94 return cell; The agent method of the Mark-tableview 98//For the selected cell (void) TableView: (UITableView *) TableView Didselectrowatindexp Ath: (Nsindexpath *) indexPath100 {101//1. Remove the current cell 102 UITableViewCell *cell = [TableView cellforrowatindexpath:index PATH];103 104//3. Add the Indexpath of the removed cell to the array and add the Auxiliary guide view to the cell if ([Self.cellindexpaths Containsobject:indexpath] )//already exists 106 {107 Cell.accessorytype = uitableviewcellaccessorynone;108 109//Remove 110 from array [Self.cellindexpaths removeobject:indexpath];111}112 else113 {cell.accessorytype = UITABLEVIEWC ELLACCESSORYCHECKMARK;115 116//Add to array 117 [Self.cellindexpaths addobject:indexpath];118}119}120 121 @end
IOS: Deleting multiple cells (method I)