Uitbableview as a list of information, in addition to the display of features, and sometimes used to delete, 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, clicking the button, you can drag the cell, move the position, and do the manual sorting.
To use the system to drag the sort function with its own steps:
1, let TableView into the editing state, that is, set its editing to Yes
2, return to edit mode, that is, to achieve the Tableview:editingstyleforrowatindexpath in Uitableviewdelegate: Method, Return the Uitableviewcelleditingstylenone mode inside. If not implemented, the default return is Delete mode
3, the implementation of the TableView:moveRowAtIndexPath:toIndexPath method, as long as the implementation of the method, you can achieve the drag sorting cell, but only to achieve the surface of the order, 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;//product array UIButton *_editBtn;//Series
The series 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"
oftype:@ "plist"]];
Save the data to the model object and then save the object to 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 returns a few lines-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (NSINteger) Section {return _goodsary.count;} #pragma mark each line display Content-(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 selected BOC-(void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) Indexpath {//uncheck
State [TableView Deselectrowatindexpath:indexpath Animated:yes]; #pragma mark set Row height-(cgfloat) TableView: (UITableView *) TableView Heightforrowatindexpath: (Nsindexpath*) Indexpath {return 110;}
#pragma mark click on the Edit button-(ibaction) CLICKEDITBTN: (UIButton *) Sender {//Set TableView edit state BOOL flag =!_tableview.editing;
[_tableview Setediting:flag Animated:yes];
_editbtn.selected = Flag; #pragma mark chooses edit mode, the add mode is rarely used, the default is Delete-(Uitableviewcelleditingstyle) TableView: (UITableView *) TableView
Editingstyleforrowatindexpath: (Nsindexpath *) Indexpath {return uitableviewcelleditingstylenone;} #pragma mark sort when a row is moved, it is called//edited, and as long as this method is implemented, the drag sort-(void) TableView: (UITableView *) TableView Moverowatindexpath: ( Nsindexpath *) Sourceindexpath Toindexpath: (Nsindexpath *) Destinationindexpath {//Remove model data to drag goods *goods = _goodsAry
[Sourceindexpath.row];
Deletes the data from the previous row [_goodsary Removeobject:goods];
Insert data into a new location [_goodsary insertobject:goods AtIndex:destinationIndexPath.row];
} @end
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.