IOS UITableViewCell Swipe to delete

Source: Internet
Author: User

In general, we use the form of the list to show the data will be used UITableView. After mastering the data with UITableView, the development process may encounter the need to delete data, we want to implement a row of data on the action, and then a delete button effect, In fact, only need to implement some of the UITableView proxy method can be.

First, we initialize an interface that is presented as 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:uitable Viewstyleplain];
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 group, 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", @ "99 99 "];
}

Now we're going to show the data:
#pragma mark-line
-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (Nsinteger) section{
return dataarray.count;
}
#pragma mark-line 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 for UITableView are implemented:
First, you have to set the cell to edit
-(BOOL) TableView: (UITableView *) TableView Caneditrowatindexpath: (Nsindexpath *) Indexpath
{
return YES;
}
Defining Edit Styles
-(Uitableviewcelleditingstyle) TableView: (UITableView *) TableView Editingstyleforrowatindexpath: (Nsindexpath *) Indexpath
{
return uitableviewcelleditingstyledelete;
}
Go to edit mode, press the Edit button that appears, 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 allows the effect of UITableViewCell sliding deletion to be achieved.

IOS UITableViewCell Swipe to delete

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.