The specific instances of deleting, inserting, and moving cells are as follows:
The code is as follows:
1 #import "ViewController.h" 2 #define NUM 3 typedef enum 4 {5 Deletecell, 6 Addcell, 7 Movecell, 8}cellstate; 9 @interface Viewcontroller () <UITableViewDataSource,UITableViewDelegate> @property (weak, nonatomic) Iboutlet UITableView *tableview; One @property (strong,nonatomic) Nsmutablearray *arraym; @property (assign,nonatomic) cellstate state; The status of the cell to be processed @end @implementation viewcontroller 16//Delete cell-(ibaction) addcellclicked: (Uibarbuttonitem *) sen Der Self.state = Addcell; self.tableView.editing =!self.tableview.editing; 21} 22//Insert cell-(ibact ION) Deletecellclicked: (Uibarbuttonitem *) sender (self.state = Deletecell; self.tableView.editing =!self . tableview.editing; 27} 28//Mobile cell-(ibaction) movecellclicked: (Uibarbuttonitem *) sender (self.state = Movecell; self.ta bleview.editing =!self.tableview.editing; (void) Viewdidload {[Super viewdidload]; 37 38//Initialization of Self.arraym = [Nsmutablearray arraywithcapacity:num]; for (int i=0; i<num; i++) $ nsstring *productname = [NSString stringwithformat:@ "product-%02d", I+1]; [Self.arraym Addobject:productname]; 44} 45 46//Set data source and agent Self.tableView.dataSource = self; Self.tableView.delegate = self; The method of #pragma mark-tableview 52//Each section has how many row Nsinteger-(TableView): (UITableView *) TableView Numberofro Wsinsection: (nsinteger) section [Self.arrayM.count]; 56} 57//Set contents of each cell-(UITableViewCell *) Tablevie W: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath 59 {60//1. According to Reuseidentifier, first go to the object pool to find the unit of reuse Lattice object, static nsstring *reuseidentifier = @ "Productcell"; UITableViewCell *cell = [Self.tableview dequeuereusablecellwithidentifier:reuseidentifier]; 63//2. If it is not found, create a Cell object by itself (cell = = nil). {$ = = [[UITableViewCell alloc]iNitwithstyle:uitableviewcellstyledefault Reuseidentifier:reuseidentifier]; 67} 68//3. Setting the contents of a Cell object cell.textLabel.text = Self.arraym[indexpath.row]; return cell; The method of #pragma mark-tableview 74//Return cell Edit Type-(Uitableviewcelleditingstyle) TableView: (UITableView *) TableView Editingstyleforrowatindexpath: (Nsindexpath *) Indexpath {self.state = = Deletecell) Uitableviewcelleditingstyledelete; Delete the "Self.state" (addcell) (= =) (+) (uitableviewcelleditingstyleinsert);//Insert 84 } The other (Uitableviewcelleditingstylenone) Move 88} 89} 90//Make edits to the cell (void) TableView: (UITableView *) TableView Commiteditingstyle: (uitableviewcelleditings Tyle) Editingstyle Forrowatindexpath: (Nsindexpath *) Indexpath 92 {93//Remove current cell 94 UITableViewCell *cell = [self.t Ableview Cellforrowatindexpath:indexpath]; if (Cell.editingstyle = = uitableviewcelledItingstyledelete)//Delete cell 96 {97//1. Delete the cell 98 [Self.arraym RemoveObjectAtIndex:indexPath.row]; 99 2. Partial refresh table [Self.tableview Deleterowsatindexpaths:@[indexpath] Withrowanimation:uitableviewrowanimationmid dle];101}102 if (Cell.editingstyle = = Uitableviewcelleditingstyleinsert)//Insert cell 103 {104//1. Create a Product resource 105 NSString *product = [NSString stringwithformat:@ "product-%02d", Arc4random_uniform (20)];106 107//2 Insert this data into the current cell 108 [Self.arraym insertobject:product atindex:indexpath.row];109 110//3. Overall Refresh Table 111 [Self.tableview reloaddata];112}113}114//Can move a-(BOOL) TableView: (UITableView *) TableView Canmoverowatindex Path: (Nsindexpath *) indexPath116 {117 if (self.state = = Deletecell | | self.state = addcell) 118 {119 Return no;120}121 else122 {123 return yes;124}125}126//Move cell 127-(void) TableView: (UITableView *) tabl Eview Moverowatindexpath:(Nsindexpath *) Sourceindexpath Toindexpath: (Nsindexpath *) destinationIndexPath128 {129//modify the order of elements in the array 130//Take out the array to move Element 131 NSString *item = [Self.arraym objectatindex:sourceindexpath.row];132 133//Delete the original position of the element 134 [Self.array M removeobjectatindex:sourceindexpath.row];135 136//Reinsert new element in new position 137 [Self.arraym Insertobject:item AtIndex: destinationindexpath.row];138}139 @end
IOS: Delete, insert, move cells