#pragmaMark-----The move operation of the table view-----//The first step in the move is also to open the edit state of the table view//2. Specify which rows can be moved-(BOOL) TableView: (UITableView *) TableView Canmoverowatindexpath: (Nsindexpath *) Indexpath {//can be moved by default returnYES;}//3. What to do after the move is complete, how to complete the mobile- (void) TableView: (UITableView *) TableView Moverowatindexpath: (Nsindexpath *) Sourceindexpath Toindexpath: (Nsindexpath *) Destinationindexpath {//record the model data in the original location firstStudent *student =_dataarray[sourceindexpath.row]; [Student retain]; //Delete model data from the original location[_dataarray RemoveObjectAtIndex:sourceIndexPath.row]; //Add the recorded model data to the data array in a new location[_dataarray insertobject:student AtIndex:destinationIndexPath.row]; [Student release];}#pragmaMark-----Delete, add data-----//1. Make the table view that you want to delete and add operations in edit State- (void) Setediting: (BOOL) editing animated: (bool) Animated {//This method in the parent class is executed first. [Super setediting:editing animated:animated]; //Table View executes this method[Self.tableview setediting:editing animated:animated];}//2. Specify which rows in the diagram can be in the edit State-(BOOL) TableView: (UITableView *) TableView Caneditrowatindexpath: (Nsindexpath *) Indexpath {//if (indexpath.row% 2 = = 0) {//return YES;//} else {//return NO;// } //all default rows can be edited returnYES;}//3, specify the editing style, whether to delete or add the delegate protocol inside-(Uitableviewcelleditingstyle) TableView: (UITableView *) TableView Editingstyleforrowatindexpath: (Nsindexpath *) Indexpath {//If this method is not overridden, the default is to delete the style//return uitableviewcelleditingstyledelete; returnUitableviewcelleditingstyleinsert;}//whether delete or add, this method is the core method of operation, when click Delete, or add a button, what needs to be done, how to complete the deletion or add the operation, all within this method is specified. - (void) TableView: (UITableView *) TableView Commiteditingstyle: (uitableviewcelleditingstyle) Editingstyle Forrowatindexpath: (Nsindexpath *) Indexpath {[TableView beginupdates];//Table View start update ( writable and writable) if(Editingstyle = =uitableviewcelleditingstyledelete) { //remove cells from this location[TableView Deleterowsatindexpaths:@[indexpath] withrowanimation:uitableviewrowanimationfade]; //Delete data from the data array that is bound to the cell[_dataarray RemoveObjectAtIndex:indexPath.row]; } Else if(Editingstyle = =Uitableviewcelleditingstyleinsert) {Student*student =_dataarray[indexpath.row]; //Building a location informationNsindexpath *index = [Nsindexpath indexpathforrow:0Insection:0]; [TableView Insertrowsatindexpaths:@[index] withrowanimation:uitableviewrowanimationtop]; [_dataarray insertobject:student AtIndex:index.row]; } [TableView endupdates];//Table View End update (same can be written without writing, if written[TableView Beginupdates],endupdates also to write]
) }
UITableView Add, delete, move operations