UITableView (4): Move cells and sections in TableView Remove cell and section from TableView

Source: Internet
Author: User

A. Question: You want to move and drag cells and sections in tableview with smooth, intuitive animations

Scheme:

Use the Movesection:tosection: method to move a section to a new position.

To move a cell from its current location to a new location using the Moverowatindexpath:toindexpath: method

Example:

Create a TableView and load 3 sections in it, with 3 cells per section

#pragma-Mark initialization Data-(Nsmutablearray *) Newsectionwithindex: (Nsuinteger) Index Withcellcount: (nsuinteger) cellcount{Nsmutablearray*result =[[Nsmutablearray alloc]init];  for(Nsuinteger counter =0; Counter < Cellcount; counter++) {[Result addobject:[[nsstring Alloc]initwithformat:@"Section %lu Cell%lu", Index,counter +1]]; }    returnresult;}- (ID) Initwithnibname: (NSString *) Nibnameornil Bundle: (NSBundle *) nibbundleornil{ Self=[Super Initwithnibname:nibnameornil Bundle:nibbundleornil]; //populating data in an instantiated method    if(Self! =Nil) {_arrayofsections=[[Nsmutablearray alloc]init]; Nsmutablearray*section1 = [Self Newsectionwithindex:1Withcellcount:3]; Nsmutablearray*section2 = [Self Newsectionwithindex:2Withcellcount:3]; Nsmutablearray*section3 = [Self Newsectionwithindex:3Withcellcount:3];        [_arrayofsections Addobject:section1];        [_arrayofsections Addobject:section2];    [_arrayofsections Addobject:section3]; }    returnSelf ;}

The above functions should be implemented in Appdelegate.

-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchOptions {     // Override point for customization after application launch.    Viewcontroller *VC = [[Viewcontroller Alloc]initwithnibname:nil Bundle:nil];     = VC    ;     return YES;}

Then create the TableView and implement the relevant Protocol method

#pragmaMark-Protocol Method-(Nsinteger) Numberofsectionsintableview: (UITableView *) tableview{return_arrayofsections.count;}-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) section{return[[_arrayofsections objectatindex:section]count];}-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{UITableViewCell*result =Nil; StaticNSString *cellidentifier =@"Cellidentifier"; Result=[TableView Dequeuereusablecellwithidentifier:cellidentifier]; if(Result = =Nil) {Result=[[UITableViewCell Alloc]initwithstyle:uitableviewcellstyledefault reuseidentifier:cellidentifier]; } Result.textLabel.text=[[_arrayofsections Objectatindex:indexpath.section]objectatindex:indexpath.row]; returnresult;}
- (void) viewdidload {[Super viewdidload]; //additional setup after loading the view, typically from a nib._mytableview =[[UITableView alloc]initwithframe:self.view.bounds style:uitableviewstylegrouped]; _mytableview.autoresizingmask= Uiviewautoresizingflexibleheight|Uiviewautoresizingflexiblewidth; _mytableview.datasource=Self ; _mytableview.Delegate=Self ;        [Self.view Addsubview:_mytableview]; Nstimer*timer = [Nstimer timerwithtimeinterval:2.0ftarget:self selector: @selector (Movesection1tosection3) Userinfo:nil Repeats:no]; [Timer fire];}

Finally implement the section and cell related movement, the function implementation code is as follows:

#pragmaHow the Mark-section and the cell are moving//Section1 move to Section3- (void) movesection1tosection3{//Data ManipulationNsmutablearray *section1 = [_arrayofsections objectatindex:0];    [_arrayofsections Removeobject:section1];    [_arrayofsections Addobject:section1]; //TableView Moving section[_mytableview movesection:0Tosection:2]; [Self movecellinsection1tocell2insection1];}//move cell with section- (void) movecellinsection1tocell2insection1{//DataNsmutablearray *section1 = [_arrayofsections objectatindex:0]; NSString*cell1insection1 = [Section1 objectatindex:0];    [Section1 Removeobject:cell1insection1]; [Section1 insertobject:cell1insection1 Atindex:1]; //View[_mytableview Moverowatindexpath:[nsindexpath Indexpathforrow:0Insection:0] Toindexpath:[nsindexpath Indexpathforrow:1Insection:0]]; [Self movecell2insection1tocell1insection2];}//different section mobile cell- (void) movecell2insection1tocell1insection2{Nsmutablearray*section1 = [_arrayofsections objectatindex:0]; Nsmutablearray*section2 = [_arrayofsections objectatindex:1]; NSString*cell2insection1 = [Section1 objectatindex:1];    [Section1 Removeobject:cell2insection1]; [Section2 insertobject:cell2insection1 Atindex:0]; [_mytableview moverowatindexpath:[nsindexpath Indexpathforrow:1Insection:0] Toindexpath:[nsindexpath Indexpathforrow:0Insection:1]];}

Two. Remove cell and section from TableView

1. Delete section:

First delete the section from the data source;

Then call TableView's instance method deletesection:withrowanimation: The first argument is a Nsindexset type, which can be instantiated by Indexsetwithindex: and Indexsetwithindexesinrange: Can

2. Delete the cell:

First, delete the data corresponding to the cell;

Deleterowsatindexpaths:withrowanimation:

#pragmaMark-Delete Cell and section//Delete section- (void) deletesection1{//Data Source[_arrayofsections Removeobjectatindex:0]; //then delete the section of TableViewNsindexset *sectiontodelete = [Nsindexset indexsetwithindex:0];    [_mytableview deletesections:sectiontodelete withrowanimation:uitableviewrowanimationautomatic]; [Self Deletecell2insection3];}//Delete cell- (void) deletecell2insection3{Nsmutablearray*section3 = [_arrayofsections objectatindex:[_arrayofsections count]-1]; [Section3 Removeobjectatindex:1]; //add multiple Indexpath to delete multiple rows    [_mytableview deleterowsatindexpaths:[nsarray arraywithobjects:[nsindexpath indexpathforrow:1 Insection:2  ], nil] withrowanimation:uitableviewrowanimationautomatic];}- (void) didreceivememorywarning {[Super didreceivememorywarning]; //Dispose of any resources the can be recreated.}

UITableView (4): Move cells and sections in TableView Remove cell and section from TableView

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.