UITableView Delete Add and move

Source: Internet
Author: User

#import "RootTableViewController.h"@interfaceRoottableviewcontroller () @property (nonatomic, strong) Nsmutablearray*Alldataarray; @property (nonatomic, assign) Uitableviewcelleditingstyle style;@end@implementationRoottableviewcontroller- (void) viewdidload {[Super viewdidload]; Self.view.backgroundColor=[Uicolor Lightgraycolor]; //set the navigation barSelf.navigationController.navigationBar.barTintColor =[Uicolor Orangecolor]; Self.title=@"Yoon-ho"; Self.navigationController.navigationBar.tintColor=[Uicolor Whitecolor]; [Self.navigationController.navigationBar settitletextattributes:@{nsforegroundcolorattributename: [Uicolor Whitecolor], nsfontattributename: [Uifont systemfontofsize: -]}]; //working with Data[self handledata]; //Register TableView[Self.tableview Registerclass:[uitableviewcellclass] Forcellreuseidentifier:@"Cell"]; //Add right buttonUibarbuttonitem *button1 =[[Uibarbuttonitem alloc] Initwithbarbuttonsystemitem:uibarbuttonsystemitemedit target:self action: @selector (        Rightbarbuttonitemclick:)]; Uibarbuttonitem*button2 =[[Uibarbuttonitem alloc] Initwithbarbuttonsystemitem:uibarbuttonsystemitembookmarks target:self action:@        Selector (Moveclick:)]; Self.navigationItem.rightBarButtonItems=@[button1, Button2]; //Add left buttonSelf.navigationItem.leftBarButtonItem =[[Uibarbuttonitem alloc] Initwithbarbuttonsystemitem:uibarbuttonsystemitemadd target:self action: @selector ( Leftbarbuttonitemclick:)];}//working with Data- (void) Handledata {//1. Initializing large arraysSelf.alldataarray =[Nsmutablearray array]; //2. Define three numbers hold the names of each group of studentsNsmutablearray *array1 = @[@"Tai Lung",@"Dao Mei",@"Card Master",@"Tim Mo",@"Ash",@"The barbarian King"].mutablecopy; Nsmutablearray*array2 = @[@"Gagne",@"Weizhuang",@"Dawn",@"Less Feather",@"High Month"].mutablecopy; Nsmutablearray*array3 = @[@"Yoon-ho",@"Yin Wei",@"Yin Yi-ho",@"Yin Dongdong",@"Yun Bu"].mutablecopy; //3. Store all students in a large array[Self.alldataarray Addobject:array1];    [Self.alldataarray Addobject:array2];    [Self.alldataarray Addobject:array3]; }#pragmaMark-table View Data source-(Nsinteger) Numberofsectionsintableview: (UITableView *) TableView {returnSelf.allDataArray.count;}-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) Section {return[Self.alldataarray[section] count];}-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath {UITableViewCell*cell = [TableView dequeuereusablecellwithidentifier:@"Cell"Forindexpath:indexpath]; //Setting up dataNsarray *array =[Self.alldataarray objectAtIndex:indexPath.section]; Cell.textLabel.text=Array[indexpath.row]; returncell;}//Set Row Height-(CGFloat) TableView: (UITableView *) TableView Heightforrowatindexpath: (Nsindexpath *) Indexpath {return  -;}//Uncheck State- (void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) Indexpath {[TableView deselectrowatindexpath:indexpath animated:yes];}#pragmaMark-Edit (delete and add)//Right button click event- (void) Rightbarbuttonitemclick: (Uibarbuttonitem *) Sender {Self.style=Uitableviewcelleditingstyledelete; //leave cell in edit state[Self.tableview setediting:!self.tableView.editing Animated:yes]; }//left button click event- (void) Leftbarbuttonitemclick: (Uibarbuttonitem *) Sender {Self.style=Uitableviewcelleditingstyleinsert; [Self.tableview setediting:!self.tableView.editing Animated:yes];}//specify which cells can be edited-(BOOL) TableView: (UITableView *) TableView Caneditrowatindexpath: (Nsindexpath *) Indexpath {if(Indexpath.section = =0|| Indexpath.section = =1) {        returnYES; }        returnNO;}//set Edit Style-(Uitableviewcelleditingstyle) TableView: (UITableView *) TableView Editingstyleforrowatindexpath: (Nsindexpath *) Indexpath {returnSelf.style;}//Finish Editing- (void) TableView: (UITableView *) TableView Commiteditingstyle: (uitableviewcelleditingstyle) Editingstyle Forrowatindexpath: (Nsindexpath *) Indexpath {if(Editingstyle = =Uitableviewcelleditingstyleinsert) {[self.alldataarray[indexpath.section] InsertObject:@"Star Soul"AtIndex:indexPath.row +1]; Nsindexpath*newindexpath = [Nsindexpath IndexPathForRow:indexPath.row +1InSection:indexPath.section];            [TableView Insertrowsatindexpaths:@[newindexpath] withrowanimation:uitableviewrowanimationtop]; } Else if(Editingstyle = =uitableviewcelleditingstyledelete)                {[self.alldataarray[indexpath.section] removeObjectAtIndex:indexPath.row];    [TableView Deleterowsatindexpaths:@[indexpath] withrowanimation:uitableviewrowanimationright]; }}#pragmaMark-Mobile//leave cell in edit state- (void) Moveclick: (Uibarbuttonitem *Sender {[Self.tableview setediting:!self.tableView.editing Animated:yes];}//set which cells can be moved-(BOOL) TableView: (UITableView *) TableView Canmoverowatindexpath: (Nsindexpath *) Indexpath {returnYES;}//Start Moving- (void) TableView: (UITableView *) TableView Moverowatindexpath: (Nsindexpath *) Sourceindexpath Toindexpath: (Nsindexpath *) Destinationindexpath {//get the data you need to modifyNSString *sourcename =[self.alldataarray[sourceindexpath.section] objectAtIndex:sourceIndexPath.row]; //remove data from the current location first[self.alldataarray[sourceindexpath.section] removeObjectAtIndex:sourceIndexPath.row]; //then insert the data into the corresponding location[self.alldataarray[destinationindexpath.section] Insertobject:sourcename AtIndex:destinationIndexPath.row]; }//prevent arbitrary Movement-(Nsindexpath *) TableView: (UITableView *) TableView Targetindexpathformovefromrowatindexpath: (Nsindexpath *) Sourceindexpath Toproposedindexpath: (Nsindexpath *) Proposeddestinationindexpath {if(Sourceindexpath.section = =proposeddestinationindexpath.section) {returnProposeddestinationindexpath; } Else {        returnSourceindexpath; }}@end

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