Uitbableview as a list of information, in addition to the features displayed, and sometimes used to delete, such as shopping carts. Delete function can directly use the system comes with the deletion function, when the horizontal swipe the cell, the right side appears the red delete button, click Delete the current cell.
To use the system's own delete function:
1, let TableView into the editing state, that is, set its editing yes
2. returns the edit mode, which is the implementation of the Tableview:editingstyleforrowatindexpath in Uitableviewdelegate: Method, in which the delete mode is returned. If not implemented, the default is to return to delete mode
3, commits the deletion operation, namely realizes Tableview:commitEditingStyle:editing Styleforrowatindexpath: method. As long as the implementation of this method, the default implementation of the system swept Delete button Delete method
4. If you want to change the delete button to Chinese, you can implement the Tableview:titlefordeleteconfirmationbuttonforrowatindexpath 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"@interfaceViewcontroller () <uitableviewdatasource, uitableviewdelegate>{UITableView*_tableview;//ListNsmutablearray*_goodsary;//Product ArrayUIButton*_EDITBTN;//Edit Button}@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; //Add titleUILabel *titlelabel = [[UILabel alloc] Initwithframe:cgrectmake (0, -, Self.view.frame.size.width, -)]; 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- -, -, -, the); [_editbtn settitle:@"Edit"Forstate:uicontrolstatenormal]; [_editbtn settitle:@"Complete"forstate:uicontrolstateselected]; _editbtn.titlelabel.font= [Uifont systemfontofsize: the]; _editbtn.backgroundcolor= [Uicolor colorwithred:0.8Green0.8Blue0.8Alpha0.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- -)]; _tableview.datasource=Self ; _tableview.Delegate=Self ; [Self.view Addsubview:_tableview]; //Fetch DataNsarray *ary = [Nsarray arraywithcontentsoffile:[[nsbundle mainbundle] Pathforresource:@"shoppinggoodslist"OfType:@"plist"]]; //Save the data to the model object and then save the object in the array_goodsary =[Nsmutablearray array]; for(intI=0; i<ary.count; i++) {Goods*good =[Goods Goodswithdic:ary[i]]; [_goodsary Addobject:good]; }}#pragmaThe mark data source returns a few lines-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) section{return_goodsary.count;}#pragmaMark per line display Content-(uitableviewcell*) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{StaticNSString *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]; returncell;}#pragmaMark Selected BOC-(void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) indexpath{//Uncheck State[TableView Deselectrowatindexpath:indexpath animated:yes];}#pragmaMark Set Line Height-(cgfloat) TableView: (UITableView *) TableView Heightforrowatindexpath: (Nsindexpath *) indexpath{return the;}#pragmaMark clicks the Edit button-(ibaction) CLICKEDITBTN: (UIButton *) Sender {//set TableView Edit StateBOOL flag =!_tableview.editing; [_tableview Setediting:flag Animated:yes]; _editbtn.selected=Flag;}#pragmaMark returns to edit mode, default to delete mode-(Uitableviewcelleditingstyle) TableView: (UITableView *) TableView Editingstyleforrowatindexpath :(Nsindexpath *) indexpath{//return uitableviewcelleditingstylenone; //return uitableviewcelleditingstyleinsert; returnUitableviewcelleditingstyledelete;}#pragmaMark commits the edit operation-(void) TableView: (UITableView *) TableView Commiteditingstyle: (uitableviewcelleditingstyle) Editingstyle Forrowatindexpath: (Nsindexpath *) indexpath{//as long as this method is implemented, the default sliding delete!!!!! is implemented. if(Editingstyle! =uitableviewcelleditingstyledelete)return; //Deleting a data Model[_goodsary RemoveObjectAtIndex:indexPath.row]; //Refresh Interface//[_tableview Reloaddata];[_tableview Deleterowsatindexpaths:@[indexpath] withrowanimation:uitableviewrowanimationfade];}#pragmaMark Delete button Chinese-(NSString *) TableView: (UITableView *) TableView Titlefordeleteconfirmationbuttonforrowatindexpath: ( Nsindexpath *) indexpath{return @"Delete";}@end////Goods.h//Shopping Cart Form Delete////Created by Jerei on 15-1-7.//Copyright (c) 2015 Jerei. All rights reserved.//#import<Foundation/Foundation.h>@interfaceGoods:nsobject@property (nonatomic, copy) NSString*icon; @property (nonatomic, copy) NSString*Name: @property (nonatomic, copy) NSString*details;-(ID) Initwithdic: (nsdictionary*) dic;+(ID) Goodswithdic: (nsdictionary*) dic;@end#import "Goods.h"@implementationGoods-(ID) Initwithdic: (Nsdictionary *) dic{if(self =[Super Init]) {Self.icon= [dic Objectforkey:@"icon"]; Self.name= [dic Objectforkey:@"name"]; Self.details= [dic Objectforkey:@"Details"]; } returnSelf ;}+(ID) Goodswithdic: (Nsdictionary *) dic{Goods*good =[[Goods alloc] initwithdic:dic]; returngood;}@end
Inquiries or technical exchanges, please join the official QQ Group: (452379712)
Jerry Education
Source:http://www.cnblogs.com/jerehedu/
This article is the copyright of Yantai Jerry Education Technology Co., Ltd. and the blog Park is shared, welcome reprint, but without the consent of the author must retain this paragraph statement, and in the article page obvious location to the original link, otherwise reserves the right to pursue legal responsibility.
IOS UITableView Delete Feature