Here's a foolproof method one, which describes the second way to delete a cell by deleting the contents of the cell: (but this situation has a small disadvantage, due to the cell reuse mechanism, if the cell content is the same, the presence of the tag will cause a false deletion)
Before deletion:
After deletion:
The analysis is as follows: (if the contents of each cell are different) how to delete the contents of the cell is a simple way, then how to implement the deletion of multiple cells?
First, define two necessary mutable arrays, one to store the initialized raw data, and the other to store the selected cells, and then extract the data from it;
Secondly, the raw data is displayed in the table through the method of the data source, and by the method of the proxy, that is, the processing of the selected cell, to add a guideline view (tag) to the selected cell, and the contents of the first selected cell to be stored in the array, (two times Select to unmark it and remove it from the array);
Finally, the original data array deletes all the selected cell contents, while the data is checked and the array is emptied directly, then the table is refreshed as a whole.
The code is as follows:
1 #import "ViewController.h" 2 #define NUM 3 4 @interface viewcontroller () <uitableviewdatasource,uitableviewd Elegate> 5 @property (weak, nonatomic) Iboutlet UITableView *tableview; 6 @property (strong,nonatomic) Nsmutablearray *products; Raw Data Stock 7 @property (strong,nonatomic) Nsmutablearray *productstore; Selected Data stock 8-(ibaction) deletebuttonclicked: (Uibarbuttonitem *) sender; 9 @end @implementation Viewcontroller-(ibaction) deletebuttonclicked: (Uibarbuttonitem *) Sender 14 {15 1. Remove all selected products from the original inventory [Self.productstore enumerateobjectsusingblock:^ (id obj, Nsuinteger idx, BOOL *stop) {17 [Self.products Removeobject:obj]; 18}]; 19 20//2. Clear the selected data inventory [Self.productstore removeallobjects]; 22 23//3. Overall refresh form [Self.tableview Reloaddata]; (void) Viewdidload {[Super viewdidload]; 28//Initialize self.products = [Nsmutablearray arraywithcap Acity:num]; Self.productstore = [NsmutaBlearray Arraywithcapacity:num]; for (int i=0; i<num; i++), {nsstring *product = [NSString stringwithformat:@ "product-%02d", I]; [Self.products addobject:product]; 35} 36 37//Set data source and agent Self.tableView.dataSource = self; Self.tableView.delegate = self; Nine} #pragma mark-tableview data source method 43//Each scetion how many row-(Nsinteger) TableView: (UITableView *) TableView Numbero Frowsinsection: (Nsinteger) section {Self.products.count return; 47} 48//Set contents of each cell-(UITableViewCell *) tab Leview: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath 50 {51//1. According to the Reuseidentifier, first go to the object pool to find The reused cell object is a static nsstring *reuseidentifier = @ "Productcell"; UITableViewCell *cell = [Self.tableview dequeuereusablecellwithidentifier:reuseidentifier]; 54//2. If it is not found, create a Cell object by itself (cell = = nil), and then set the cells = [[UITableViewCell alloc]initwithstyle:uitabl Eviewcellstyledefault ReuseidenTifier:reuseidentifier]; 58} 59//3. Set the contents of a Cell object cell.textLabel.text = Self.products[indexpath.row]; 61//Set Font Color Cell.textLabel.textColor = [Uicolor Redcolor]; 63//Set Font size Cell.textLabel.font = [Uifont systemfontofsize:20]; 65//Set cell color Cell.tintcolor = [Uicolor Orangecolor]; if ([Self.productstore containsobject:self.products[indexpath.row])//First selected 69 {70//Add tag Display 71 Cell.accessorytype = Uitableviewcellaccessorycheckmark; *//two times check 74 {75//unmark display cell.accessorytype = Uitableviewcellaccessorynone; 77 } The return cell; The agent method of the #pragma mark-tableview 82//The processing of the cell when selected (void) TableView: (UITableView *) TableView DIDSELECTROWATINDEXPA Th: (Nsindexpath *) Indexpath 84 {85//Get the currently selected cell UITableViewCell *cell = [TableView cellforrowatindexpath:index Path]; 87 88//Remove product from cell nsstring *product = Self.products[indexpath.row]; 90 91//Add an auxiliary guide view to the selected cells and store the product in an array ([Self.productstore containsobject:product])//Selected once 93 {94 Unmark Cell.accessorytype = Uitableviewcellaccessorynone; 96 97//Remove the product from the storage array 98 [Self.productstore removeobject:product]; }100 Else//First check 101 {102//Add tag 103 Cell.accessorytype = Uitableviewcellaccessorycheckmar K;104 105//Add product to storage array 106 [Self.productstore addobject:product];107}108}109 @end
IOS: Deletion of multiple cells (method two):