UITableView Add and delete, move

Source: Internet
Author: User

Review:

1, add a uitableview on the controller, temporarily the UITableView control variable name is TableView, set the control agent, implement the Uitableviewdatasource of the controller , Uitableviewdelegate agreement;

2, the TableView control The editing property defaults to No, and UITableViewCell does not remove and add functionality by default.

Implementing Proxy methods

-(void) TableView: (UITableView *) TableView Commiteditingstyle: (uitableviewcelleditingstyle) Editingstyle Forrowatindexpath: (Nsindexpath *) Indexpath;

Then the Delete button appears when the UITableViewCell is dragged to the left:

In the proxy method to do the corresponding processing, you can implement the deletion function, the code is as follows:

Proxy method, which can be implemented to add cell lines or delete cell rows-(void) TableView: (UITableView *) TableView Commiteditingstyle: ( Uitableviewcelleditingstyle) Editingstyle Forrowatindexpath: (Nsindexpath *) indexpath{//    typedef NS_ENUM ( Nsinteger, Uitableviewcelleditingstyle) {//        uitableviewcelleditingstylenone,//        Uitableviewcelleditingstyledelete,  //= delete//        Uitableviewcelleditingstyleinsert   //= Add//    };   NSLog (@ "%d", editingstyle);    When the style is deleted, delete the    if (Editingstyle = = uitableviewcelleditingstyledelete) {                //delete one row in the array        [self.arrays[ Indexpath.section] RemoveObjectAtIndex:indexPath.row];        [TableView reloaddata];//Remove all reload        [TableView Deleterowsatindexpaths:@[indexpath] Withrowanimation: uitableviewrowanimationright];//only flushes the Delete rows section (better performance)    }    }

  

There is also a proxy method:

-(uitableviewcelleditingstyle) TableView: (uitableview *) TableView Editingstyleforrowatindexpath: ( Nsindexpath *) Indexpath; When this method is not implemented, the default is to return

Uitableviewcelleditingstyledelete enumeration, implement this method if you want to increase the cell, and return the Uitableviewcelleditingstyleinsert enumeration

Then also set the TableView control property editing to Yes, the complete code is as follows:

-(void) viewdidload {[Super viewdidload];  ...//Implement code//Set TableView control editing Property tableview.editing = YES; Set the editable}//delegate proxy method, implement this method, you can set the UITableViewCell add or remove function, if you do not implement this method, the default is to delete the style-(Uitableviewcelleditingstyle)  TableView: (UITableView *) TableView Editingstyleforrowatindexpath: (Nsindexpath *) indexpath{if (indexpath.row% 2 = =    0) {return uitableviewcelleditingstyleinsert;    } else{return uitableviewcelleditingstyledelete; }}//processing UITableViewCell Add and remove functions-(void) TableView: (UITableView *) TableView Commiteditingstyle: ( Uitableviewcelleditingstyle) Editingstyle Forrowatindexpath: (Nsindexpath *) indexpath{//typedef NS_ENUM (NSInteger,        Uitableviewcelleditingstyle) {//uitableviewcelleditingstylenone,//uitableviewcelleditingstyledelete,//       uitableviewcelleditingstyleinsert//}; Delete operation if (Editingstyle = = uitableviewcelleditingstyledelete) {[self.arrays[indexpath.section] Removeobj Ectatindex:indexpatH.row]; [TableView Reloaddata]; Reload all [TableView Deleterowsatindexpaths:@[indexpath] withrowanimation:uitableviewrowanimationright];//Delete Row}//        Insert operation else if (Editingstyle = = Uitableviewcelleditingstyleinsert) {Tanperson *per = [[Tanperson alloc] init];        Per.name = @ "haha haha";        Per.address = @ "Small building last night and East Wind";        [Self.arrays[indexpath.section] Insertobject:per AtIndex:indexPath.row];    [TableView Insertrowsatindexpaths:@[indexpath] withrowanimation:uitableviewrowanimationleft]; }}

After running a load as:

You can add or remove operations:

3, UITableViewCell Mobile: To implement a proxy method, you can move the cell:

By implementing this method, you can move the cell by keeping the data and style movement consistent-(void) TableView: (UITableView *) TableView Moverowatindexpath: (Nsindexpath *) Sourceindexpath Toindexpath: (Nsindexpath *) destinationindexpath{   //NSLog (@ "moved");    Tanperson *sourceperson = Self.arrays[sourceindexpath.section][sourceindexpath.row];    Tanperson *destperson = Self.arrays[destinationindexpath.section][destinationindexpath.row];        Self.arrays[destinationindexpath.section][destinationindexpath.row] = Sourceperson;    Self.arrays[sourceindexpath.section][sourceindexpath.section] = Destperson;}

Press and hold the UITableViewCell that you want to move to the three-horizontal icon to move

UITableView Add and delete, 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.