////MAINVIEWCONTROLLER.M//editor of Ui10_tableview////Created by Dllo on 15/8/11.//Copyright (c) 2015 Zhozhicheng. All rights reserved.//#import "MainViewController.h" @interface mainviewcontroller () <Uitableviewdatasource, Uitableviewdelegate>@property(nonatomic, retain)Nsmutablearray*arr;@property(nonatomic, retain)UITableView*tableview;@end @implementation mainviewcontroller -(void) dealloc{[_arr release]; [_tableview release]; [SuperDealloc];} -(Instancetype) Initwithnibname: (NSString*) Nibnameornil Bundle: (NSBundle*) nibbundleornil{ Self=[SuperInitwithnibname:nibnameornil Bundle:nibbundleornil];if( Self) { Self. Arr= [Nsmutablearrayarraywithobjects:@"Song Jiang", @"Lu Junyi", @"WU use", @"Gongsun Win", @"Guan Sheng", @"Lin", @"Qin Ming",@"Huyan Burns", @"Renewest",@"Chai Jin", @"Li Chun", @"Zhu Yu",@"Lu Zhishen",@"Wu Song",Nil]; }return Self;} - (void) Viewdidload {[SuperViewdidload];additional setup after loading the view. Self. Navigationcontroller. Navigationbar. Translucent=NO; Self. TableView=[[UITableViewAlloc] Initwithframe:cgrectmake (0,0, Self. View. Frame. Size. Width, Self. View. Frame. Size. Height- -) Style:uicontrolstatenormal]; [ Self. ViewAddsubview: Self. TableView]; [ Self. TableViewRelease]; Self. TableView. Delegate= Self; Self. TableView. DataSource= Self;//Edit button Self. Navigationitem. Rightbarbuttonitem= Self. Editbuttonitem;//// Open the TableView's editable mode directly//[Self.tableview setediting:yes Animated:yes];}#pragma mark rewrite the system's edit button click on the Trigger method-(void) Setediting: (BOOL) Editing animated: (BOOL) animated{[SuperSetediting:editing animated:animated]; [ Self. TableViewSetediting:editing Animated:YES];}#pragma mark sets which lines can be edited-(BOOL) TableView: (UITableView*) TableView Caneditrowatindexpath: (Nsindexpath*) indexpath{////odd line editable, even number not available//if (indexpath.row% 2 = = 1) {//return YES;//}else{//return NO;// } return YES;}//Delete-(Uitableviewcelleditingstyle) TableView: (UITableView*) TableView Editingstyleforrowatindexpath: (Nsindexpath*) indexpath{returnUitableviewcelleditingstyledelete;}//Delete data-(void) TableView: (UITableView*) TableView Commiteditingstyle: (uitableviewcelleditingstyle) Editingstyle Forrowatindexpath: (Nsindexpath*) indexpath{if(Editingstyle = = Uitableviewcelleditingstyledelete) {//Delete data source first[ Self. ArrRemoveobjectatindex:indexpath. Row];//[Self.tableview reloaddata]; //Delete the cell above by TableView ///First parameter: Specify which row of the partition to delete, and put it as an element in the array ///second parameter: Delete an animation[ Self. TableViewDeleterowsatindexpaths:@[indexpath] Withrowanimation:uitableviewrowanimationleft]; }}//Modify the caption of the delete button-(NSString*) TableView: (UITableView*) TableView Titlefordeleteconfirmationbuttonforrowatindexpath: (Nsindexpath*) indexpath{return@"Come on, let me be.";}//Sticky and delete iOS8.0 only-(Nsarray*) TableView: (UITableView*) TableView Editactionsforrowatindexpath: (Nsindexpath*) indexpath{uitableviewrowaction *deleteaction=[uitableviewrowaction Rowactionwithstyle: Uitableviewrowactionstyledefault title:@"Delete"handler:^ (Uitableviewrowaction *action,Nsindexpath*indexpath) {//button click on the event to be triggered is written in block NSLog(@"triggered Delete"); }]; Deleteaction. BackgroundColor=[UicolorCyancolor]; Uitableviewrowaction *topaction=[uitableviewrowaction Rowactionwithstyle:uitableviewrowactionstyledefault title:@"Sticky"handler:^ (Uitableviewrowaction *action,Nsindexpath*indexpath) {NSLog(@"Zhiding");NSString*temp= Self. Arr[Indexpath. Row]; [ Self. ArrRemoveobjectatindex:indexpath. Row]; [ Self. ArrInsertobject:temp Atindex:0]; [ Self. TableViewReloaddata]; }]; Topaction. BackgroundColor=[UicolorRedcolor];return@[deleteaction,topaction];}//Mobile-(void) TableView: (UITableView*) TableView Moverowatindexpath: (Nsindexpath*) Sourceindexpath Toindexpath: (Nsindexpath*) destinationindexpath{//Get the data to the starting position first NSString*str=[ Self. Arr[Sourceindexpath. Row] retain];//2. Removing the starting Position object from the data source[ Self. ArrRemoveobjectatindex:sourceindexpath. Row];//3. Inserting data into the destination of an array[ Self. ArrInsertobject:str Atindex:destinationindexpath. Row]; [str release];} -(Nsinteger) TableView: (UITableView*) TableView numberofrowsinsection: (Nsinteger) section{return Self. Arr. Count;} -(UITableViewCell*) TableView: (UITableView*) TableView Cellforrowatindexpath: (Nsindexpath*) indexpath{Static NSString*[email protected]"Reuse";UITableViewCell*cell =[tableview Dequeuereusablecellwithidentifier:reuse];if(!cell) {Cell =[[[UITableViewCellAlloc] initwithstyle:uitableviewcellstylevalue1 Reuseidentifier:reuse] autorelease]; } cell. Textlabel. Text= Self. Arr[Indexpath. Row];returnCell;} - (void) Didreceivememorywarning {[SuperDidreceivememorywarning];//Dispose of any resources, can be recreated.}/ * #pragma mark-navigation//in a storyboard-based application, you'll often want to do a little preparation before navigation-(void) Prepareforsegue: (Uistoryboardsegue *) Segue Sender: (ID) Sender {//Get the new view controller using [Segue Destinationviewcontroller]. Pass the selected object to the new view Controller.} */@end
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
IOS Ui10_tableview for editing