IOS UITableView Delete Feature

Source: Internet
Author: User

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, return to the editing mode, that is, the implementation of Uitableviewdelegate in the Tableview:editingstyleforrowatindexpath: method, in the back of the delete mode. 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" @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 returns to edit mode, default to delete mode-(Uitableviewcelleditingstyle) TableView: (UITableView *) TableView    Editingstyleforrowatindexpath: (Nsindexpath *) indexpath{//return uitableviewcelleditingstylenone;    return uitableviewcelleditingstyleinsert; return uitableviewcelleditingstyledelete;} #pragma Mark commits an 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;        Delete data model [_goodsary RemoveObjectAtIndex:indexPath.row];        Refresh interface//[_tableview reloaddata]; [_tableview Deleterowsatindexpaths:@[indExpath] Withrowanimation:uitableviewrowanimationfade];} #pragma mark 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> @interface goods: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" @implementation goods-(        ID) Initwithdic: (nsdictionary *) dic{if (self = [super init]) {Self.icon = [dic objectforkey:@ "icon"];        Self.name = [dic objectforkey:@ "name"];    Self.details = [dic objectforkey:@ "Details"]; } return self;}    + (ID) goodswithdic: (nsdictionary *) dic{Goods *good = [[Goods alloc] initwithdic:dic]; return good;} @end

IOS UITableView Delete Feature

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.