UITableViewCell sliding Delete instance in iOS

Source: Internet
Author: User

In general, we use the form of a list to display the data will use the UITableView. After mastering the data with UITableView, you may encounter the need to delete the data in the development process, we want to implement a row of data on the move, and then a delete button effect, In fact, only need to implement some of the UITableView proxy method on it.

First, we initialize an interface to display in the form of a list

#pragma mark-Initialize UI
-(void) initui{
Self.view.backgroundColor = RGB (242, 242, 247);
Self.automaticallyadjustsscrollviewinsets = NO;
Sidesliptableview = [[UITableView alloc] Initwithframe:cgrectmake (0, Kscreenwidth, kScreenHeight-60) style: Uitableviewstyleplain];
Sidesliptableview.backgroundcolor = [Uicolor Clearcolor];
Sidesliptableview.delegate = self;
Sidesliptableview.datasource = self;
Sidesliptableview.separatorstyle = Uitableviewcellseparatorstylenone;
[Self.view Addsubview:sidesliptableview];
}
Then, prepare the data source


@interface Viewcontroller () <uitableviewdatasource,uitableviewdelegate>{
UITableView *sidesliptableview;
Variable groups, for deleting data
Nsmutablearray *dataarray;

}

@end

@implementation Viewcontroller

-(void) Viewdidload {
[Super Viewdidload];

[Self initui];
DataArray = [Nsmutablearray arraywitharray: @[@ "1111", @ "2222", @ "3333", @ "4444", @ "5555", @ "6666", @ "7777", @ "8888", @ " 9999 "]];
}

Next we're going to show the data.


#pragma mark-Okay.
-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (Nsinteger) section{
return dataarray.count;
}
#pragma mark-row height
-(CGFloat) TableView: (UITableView *) TableView Heightforrowatindexpath: (Nsindexpath *) indexpath{
return 46;
}
#pragma mark-cell content
-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexPath{
static NSString *indefier = @ "Cell";
Sidesliptableviewcell *cell = [TableView dequeuereusablecellwithidentifier:indefier];
if (!cell) {
cell = [[Sidesliptableviewcell alloc] Initwithstyle:uitableviewcellstyledefault reuseidentifier:indefier];
}
Cell.selectionstyle = Uitableviewcellselectionstylenone;
Cell.lable.text = Dataarray[indexpath.row];
return cell;
}

Finally, some proxy methods of implementing UITableView

Let's set up a cell to edit.
-(BOOL) TableView: (UITableView *) TableView Caneditrowatindexpath: (Nsindexpath *) Indexpath
{
return YES;
}
Define edit Style
-(Uitableviewcelleditingstyle) TableView: (UITableView *) TableView Editingstyleforrowatindexpath: (NSIndexPath *) Indexpath
{
return uitableviewcelleditingstyledelete;
}
Go to edit mode, press the Edit button that appears, and then delete the operation
-(void) TableView: (UITableView *) TableView Commiteditingstyle: (uitableviewcelleditingstyle) Editingstyle Forrowatindexpath: (Nsindexpath *) Indexpath
{
if (Editingstyle = = Uitableviewcelleditingstyledelete) {
[DataArray RemoveObjectAtIndex:indexPath.row];
Delete the row from the data source.
[Sidesliptableview Deleterowsatindexpaths:[nsarray Arraywithobject:indexpath] Withrowanimation: Uitableviewrowanimationfade];
}
}
Modify Edit button text
-(NSString *) TableView: (UITableView *) TableView Titlefordeleteconfirmationbuttonforrowatindexpath: (NSIndexPath *) Indexpath
{
return @ "Delete";
}

This will enable the UITableViewCell sliding Delete effect.

Related Article

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.