UITableViewCell cell Delete, insert, move

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.