Then the above item, when the code below marked red, we can insert, delete, move the cell by pressing the Edit button in the upper right corner.
#import "WJJRootViewController.h" @interface Wjjrootviewcontroller () {///data source holds data Nsmutablearray * _dataarray; This is our tableView UITableView * _tableview; Page Controller Uipagecontrol * _PAGECONTROL;} @end @implementation wjjrootviewcontroller-(ID) initwithnibname: (NSString *) Nibnameornil Bundle: (NSBundle *) nibbundleornil{self = [super Initwithnibname:nibnameornil Bundle:nibbundleornil]; if (self) {//Custom initialization} return to self;} -(void) viewdidload{[Super viewdidload];//do any additional setup after loading the view. Self.title = @ "2"; Simulation gets data [self createdatasources]; Create TableView [self createtableview]; Create a Barbutton to edit each cell's [self createbarbuttonitem];} The implementation of the data method is to add 20 string objects in the array-(void) createdatasources{_dataarray = [[Nsmutablearray alloc] init]; for (int i = 0; i <; i++) {NSString * tempstr = [NSString stringwithformat:@ "line%d", I]; [_dataarray ADDOBJECT:TEMPSTR]; }}//Create a TableView-(void) createtableview{/* uitableviewstyleplain non-grouped table uitableviewstylegrouped grouped TableView */ _tableview = [[UITableView alloc] Initwithframe:cgrectmake (0, 0, 320, 480) Style:uitableviewstyleplain]; After writing TableView must write down these two sentences on the agent and the data source are self _tableview.delegate = self; _tableview.datasource = self; [Self.view Addsubview:_tableview]; Create a background view UIView * Backgroudview = [[UIView alloc] Initwithframe:cgrectmake (0, 0, 320, 150)]; Create a scrolling view Uiscrollview * ScrollView = [[Uiscrollview alloc] Initwithframe:cgrectmake (0, 0, 320, 150)]; Sets the actual size of the scrolling view scrollview.contentsize = Cgsizemake (4 * 320, 150); Offset Scrollview.contentoffset = Cgpointzero; Whether paging scrollview.pagingenabled = YES; Horizontal scroll bar hidden scrollview.showshorizontalscrollindicator = NO; Set proxy scrollview.delegate = self; There's no such thing as a rubber band effect scrollview.bounces = no; Add four images to ScrollView above for (int i = 0; I < 4; i++) {Uiimageview * Imagev = [[Uiimageview alloc] Initwithframe:cgrectmake (i * 320, 0, 320, 150)]; [Imagev setimage:[uiimage imagenamed:[nsstring stringwithformat:@ "Image%d.png", I]]; [ScrollView Addsubview:imagev]; }//Initialize page Controller The total number of pages is 4 the initial page is 0 _pagecontrol = [[Uipagecontrol alloc] Initwithframe:cgrectmake (0, 130, 320, 20)]; _pagecontrol.numberofpages = 4; _pagecontrol.currentpage = 0; Put the scrolling view on the background view [Backgroudview Addsubview:scrollview]; Put the paging controller side to the background view [Backgroudview Addsubview:_pagecontrol]; Then we create the background view square in the position of the head view of TableView _tableview.tableheaderview = Backgroudview; }<span style= "color: #FF0000;" >-(void) createbarbuttonitem{//Create a button click to make TableView in edit state UIButton * Rightbutton = [UIButton buttonwithtype : Uibuttontypesystem]; Rightbutton.frame = CGRectMake (0, 0, 50, 30); [Rightbutton settitle:@ "edit" forstate:uicontrolstatenormal]; [Rightbutton addtarget:self Action: @selectoR (Edittableview) Forcontrolevents:uicontroleventtouchupinside]; Uibarbuttonitem * Rightbarbuttonitem = [[Uibarbuttonitem alloc] Initwithcustomv Iew:rightbutton]; Self.navigationItem.rightBarButtonItem = Rightbarbuttonitem;} Makes the editing state of the tableView switch between each other-(void) edittableview{_tableview.editing =!_tableview.editing;} </span> #pragma mark--uiscrollviewdelegate--//implements the proxy method for ScrollView//This method is always called as long as ScrollView is moving-(void) Scrollviewdidscroll: (Uiscrollview *) scrollview{//Because UITableView is a subclass of Uiscrollview all judgment we are clicking on which ScrollView if (SCR Ollview = = _tableview) {//tableview offset is just above y so when the offset is greater than 151 let our navigation bar fade away if (Scrollview.contentoffset.y > 151) {[UIView animatewithduration:1 animations:^{Self.navigationController.navigationBar.alpha = 0; }]; }//When the offset is less than 86 o'clock let our navigation bar show up slowly else if (Scrollview.contentoffset.y <) {[UIView animatewithdu RaTion:2 animations:^{//self.navigationcontroller.navigationbarhidden = NO; Self.navigationController.navigationBar.alpha = 1; }]; }}}//This is the last function in ScrollView's proxy method to stop slowing down. Here we set the page controller and the ScrollView Association when the scrollview reaches a certain offset, the other page controller also changes-(void) SCR Ollviewdidenddecelerating: (Uiscrollview *) scrollview{//When we click on TableView, do not operate if (ScrollView = = _tableview) { }//At that time ScrollView we calculated to change the page value of Pagecontrol else{nsinteger page = scrollview.contentoffset.x/320; _pagecontrol.currentpage = page; }} #pragma mark--uitableviewdeleagate//This method must be implemented to return the number of rows in a group because we are in a group so we return the data source array directly-(Nsinteger) TableView: ( UITableView *) TableView numberofrowsinsection: (nsinteger) section{return _dataarray.count;} This method, like the one above, must be implemented to return the proxy method of cell reuse cell-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatin Dexpath: (Nsindexpath *) indexpath{//First TableView will find the unused cell snail with the tag @ "Uitabl"Eviewcell * Tableviewcell = [TableView dequeuereusablecellwithidentifier:@ "snail"]; If not, it is automatically created and the tag @ "snail" is written on if (!tableviewcell) {Tableviewcell = [[UITableViewCell alloc] Initwithstyle:uita Bleviewcellstylesubtitle reuseidentifier:@ "snail"]; }//Indexpath has two parts: a row Indexpath.row another is: a group of indexpath.section//cell above the Textlabel plus our array corresponding to the string Tableviewcell . Textlabel.text = _dataarray[indexpath.row]; Add a picture to the cell [Tableviewcell.imageview setimage:[uiimage imagenamed:@ "[email protected]"]; This gives the cell Details tab to add text TableViewCell.detailTextLabel.text = _dataarray[indexpath.row]; return Tableviewcell;} Returns the height of the Tableviewcell-(cgfloat) TableView: (UITableView *) TableView Heightforrowatindexpath: (Nsindexpath *) indexpath{return 70;} Check the method called by each Tableviewcell-(void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) indexpath{//Click cell to make cell not clicked and animate [TableView Deselectrowatindexpath:indexpath animated:yes];} &lT;span style= "color: #FF0000;" > #warning the proxy method for manipulating the cell//This method is to set whether each TableView can be edited Yes can be edited-(BOOL) TableView: (UITableView *) TableView Caneditrowatindexpath: (Nsindexpath *) indexpath{return YES;} Set each cell if the editor will perform what edits-(uitableviewcelleditingstyle) TableView: (UITableView *) TableView Editingstyleforrowatindexpath: (Nsindexpath *) indexpath{//When the number of rows is even, we set this cell if the action is to delete the action if (indexpath.row% 2 = = 0) {return uitableviewcelleditingstyledelete; }else{//odd when inserted operation return uitableviewcelleditingstyleinsert; }}//This method is performed according to the operation form set for each cell above for the actual operation-(void) TableView: (UITableView *) TableView Commiteditingstyle: ( Uitableviewcelleditingstyle) Editingstyle Forrowatindexpath: (Nsindexpath *) indexpath{//If the cell is a deleted form then we will delete this cell if (Editingstyle = = Uitableviewcelleditingstyledelete) {//First remove the data from the data source [_dataarray Removeobjectatindex:in Dexpath.row]; Then TableView in deleting an action painting can be many kinds of [TableView Deleterowsatindexpaths:@[indexpath] WithrOwanimation:uitableviewrowanimationright]; }else if (Editingstyle = = Uitableviewcelleditingstyleinsert) {//If it is an insert operation//record the position of the inserted NSI Nteger row = Indexpath.row; The information that will be displayed on the cell after inserting the cell NSString * insertstr = @ "Add line"; This data is first added to the data source [_dataarray Insertobject:insertstr Atindex:row]; Then insert a row on the TableView [TableView Insertrowsatindexpaths:@[indexpath] Withrowanimatio N:uitableviewrowanimationright]; }}//set the string to display when each cell moves to the left-(NSString *) TableView: (UITableView *) TableView Titlefordeleteconfirmationbuttonforrowatindexpath: (Nsindexpath *) indexpath{return @ "delete";} #warning the proxy method for cell movement//Set whether the cell can be moved-(BOOL) TableView: (UITableView *) TableView Canmoverowatindexpath: (Nsindexpath *) indexpath{return YES; Move the cell Sourceindexpath source cell location Destinationindexpath destination-(void) TableView: (UITableView *) TableView Moverowatindexpath: (Nsindexpath *) sourceindexpAth Toindexpath: (Nsindexpath *) destinationindexpath{//record which line of cell to move nsinteger fromrow = Sourceindexpath.row; Record what line the cell will be moved to nsinteger torow = Destinationindexpath.row; Take out the data that you want to move. ID obj = _dataarray[fromrow]; The row to be moved corresponds to the data deletion in the array [_dataarray removeobject:obj]; Then move the target location corresponding to the array data source to add the data [_dataarray insertobject:obj Atindex:torow]; Then let TableView re-load the data [TableView Reloaddata]; }</span>-(void) didreceivememorywarning{[Super didreceivememorywarning]; Dispose of any resources the can be recreated.} @end
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Snail-ui Learning Table View TableView (ii)