Methods of Uitableviewdelegate Setedit Mode
The edit style of the cell in(delete or insert)-(Uitableviewcelleditingstyle)TableView:(UITableView *) TableViewEditingstyleforrowatindexpath:(Nsindexpath *) Indexpath; methods of Uitableviewdatasource
Sets whether the cell can be edited -(BOOL) TableView: (UITableView *) TableViewCaneditrowatindexpath:(Nsindexpath *) Indexpath;
Sets whether the cell can be moved -(BOOL)TableView:(UITableView *) TableViewCanmoverowatindexpath:(Nsindexpath *) Indexpath; to edit a cellThis method is called when the Delete Cells: 1) FirstDeleteData source 2) NextDeleteCell 3) Refresh cell Insert Cell: 1) FirstInsertData source 2) NextInsertCell 3) Refresh cells-(void)TableView:(UITableView *) TableViewCommiteditingstyle:(Uitableviewcelleditingstyle) EditingstyleForrowatindexpath:(Nsindexpath *) indexpath{//Judging edit style (delete or insert)if (editingstyle==Uitableviewcelleditingstyledelete) { //must delete data first Source
nsmutablearray *arr=[self. Datasourcearray objectatindex: Indexpath. Section ];
[Arr removeobjectatindex: Indexpath. Row];
Then delete the cell(the parameter is an array and may delete multiple rows)
[TableViewdeleterowsatindexpaths:[Nsarray Arraywithobject:Indexpath]withrowanimation: Uitableviewrowanimationfade];//Delete to refresh TableView when finished
[TableViewReloaddata];
} else if (Editingstyle==uitableviewcelleditingstyleinsert)//Insert mode
{//below according to the actual situation
Csfriends *friend=[[csfriends Alloc]init];
Friend.imagename=[nsstring stringwithformat:@ "%d.jpg", 2];
[Email protected] "Super Broly";
[Email protected] "super-Asian";
You must first insert a data source
Nsmutablearray*arr=[ Self.DatasourcearrayObjectatindex: Indexpath. Section];[ArrInsertObject: FriendAtindex: Indexpath.row]; Then insert the cell(the argument is an array, possibly inserting multiple lines)
[TableViewinsertrowsatindexpaths:[Nsarray Arraywithobject: Indexpath]withrowanimation: Uitableviewrowanimationmiddle]; To refresh TableView after inserting
[TableView Reloaddata];
}}
This method is called when a cell is moved
Move a Cell: 1) The data to be moved first fromin the original location of the data sourceDelete2) Then we'll talk about the data.Insert a new location for the data source3) Refresh Cells
-(void)TableView:(UITableView *) TableViewMoverowatindexpath:(Nsindexpath *) SourceindexpathToindexpath:(Nsindexpath *) destinationindexpath{first find the data to be moved (Sourceindexpath is the position before the move)Nsmutablearray*array= Self.Datasourcearray[Sourceindexpath. Section]; Csfriends*friend= array[Sourceindexpath. row];//remove this data from the data source[ArrayRemoveobject: friend];
//Find a new location data source(Destinationindexpath is the position after the move)
Nsmutablearray*array= Self.Datasourcearray[Destinationindexpath. Section];//Add data to a new location in the data source first
[ArrayInsertObject: FriendAtindex:Destinationindexpath.Row];//Last Refresh cell
[TableViewReloaddata];}
UITableViewCell cell Delete, insert, move