Uitbableview as a list of information, in addition to the functionality of the display, and sometimes use the deletion, sorting and other functions, the following to explain how to achieve sorting.
Sorting is when the table enters the edit state, a button appears on the right side of the cell, click the button, you can drag the cell, move the position, to manually sort.
To use the system's own drag sorting feature:
1, let TableView into the editing state, that is, set its editing yes
2, return to the editing mode, that is, to implement Uitableviewdelegate in the Tableview:editingstyleforrowatindexpath: method, Returns the Uitableviewcelleditingstylenone mode inside. If not implemented, the default is to return to delete mode
3, implement the TableView:moveRowAtIndexPath:toIndexPath method, as long as the implementation of this method, you can achieve the drag sorting of cells, but only to achieve the surface of the sorting, and did not modify the real data
4. Complete the update of the data model in the method
Code:
viewcontroller.m//Jrtableview Delete////Created by jerehedu on 15/6/11.//Copyright (c) 2015 jerehedu. All rights reserved.//#import "ViewController.h" #import "Goods.h" @interface Viewcontroller () < Uitableviewdatasource, uitableviewdelegate>{UITableView *_tableview;//list Nsmutablearray *_goodsAry;//commodity array UIButton *_editbtn; Edit button} @end @implementation viewcontroller-(void) viewdidload {[Super viewdidload]; Add title UILabel *titlelabel = [[UILabel alloc] Initwithframe:cgrectmake (0, Self.view.frame.size.width, 44)]; Titlelabel.text = @ "Shopping cart"; Titlelabel.textalignment = Nstextalignmentcenter; Titlelabel.backgroundcolor = [Uicolor Redcolor]; Titlelabel.textcolor = [Uicolor Whitecolor]; [Self.view Addsubview:titlelabel]; Add Edit button _editbtn = [UIButton buttonwithtype:uibuttontypecustom]; _editbtn.frame = CGRectMake (self.view.frame.size.width-60, 25, 50, 34); [_editbtn settitle:@ "edit" forstate:uicontrolstatenormal]; [_eDITBTN settitle:@ "complete" forstate:uicontrolstateselected]; _editbtn.titlelabel.font = [Uifont systemfontofsize:15]; _editbtn.backgroundcolor = [Uicolor colorwithred:0.8 green:0.8 blue:0.8 alpha:0.5]; [Self.view ADDSUBVIEW:_EDITBTN]; [_editbtn addtarget:self Action: @selector (CLICKEDITBTN:) forcontrolevents:uicontroleventtouchupinside]; Add TableView _tableview = [[UITableView alloc] Initwithframe:cgrectmake (0, +, Self.view.frame.size.width, Self.view. FRAME.SIZE.HEIGHT-64)]; _tableview.datasource = self; _tableview.delegate = self; [Self.view Addsubview:_tableview]; Fetch data Nsarray *ary = [Nsarray arraywithcontentsoffile:[[nsbundle mainbundle] pathforresource:@ "ShoppingGoodsList" ofTy pe:@ "Plist"]; The data is stored in the model object, and then the object is stored in the array _goodsary = [Nsmutablearray array]; for (int i=0; i<ary.count; i++) {Goods *good = [Goods goodswithdic:ary[i]]; [_goodsary Addobject:good]; }} #pragma the mark data source has returned a few lines-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) section{return _goodsary.count;} #pragma mark displays content per line-(uitableviewcell*) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{static NSString *idgood = @ "goods"; UITableViewCell *cell = [TableView Dequeuereusablecellwithidentifier:idgood]; if (cell==nil) {cell = [[UITableViewCell alloc] Initwithstyle:uitableviewcellstylesubtitle Reuseidentifier:idgood] ; } Goods *good = _goodsary[indexpath.row]; Cell.imageView.image = [UIImage ImageNamed:good.icon]; Cell.textLabel.text = Good.name; Cell.detailTextLabel.text = good.details; Cell.detailTextLabel.numberOfLines = 6; Cell.detailTextLabel.textColor = [Uicolor Browncolor]; return cell;} #pragma mark Select BOC-(void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) indexpath{//Unchecked [TableView Deselectrowatindexpath:indexpath animated:yes];} #pragma mark set line height-(cgfloat) TableView: (UITabLeview *) TableView Heightforrowatindexpath: (Nsindexpath *) indexpath{return 110;} #pragma mark click the Edit button-(ibaction) CLICKEDITBTN: (UIButton *) Sender {//Set TableView Edit Status BOOL flag =!_tableview.editi Ng [_tableview Setediting:flag Animated:yes]; _editbtn.selected = Flag;} #pragma mark Select edit mode, add mode is rarely used, default is Delete-(Uitableviewcelleditingstyle) TableView: (UITableView *) TableView Editingstyleforrowatindexpath: (Nsindexpath *) indexpath{return uitableviewcelleditingstylenone;} #pragma mark sort is called when a row is moved//edit state, as long as the implementation of this method, you can achieve drag sorting-(void) TableView: (UITableView *) TableView Moverowatindexpath: ( Nsindexpath *) Sourceindexpath Toindexpath: (Nsindexpath *) destinationindexpath{//Remove model data to drag Goods *goods = _goodsAr Y[sourceindexpath.row]; Delete data from previous rows [_goodsary removeobject:goods]; Insert data into the new location [_goodsary insertobject:goods atIndex:destinationIndexPath.row];} @end
IOS UITableView Drag sort function