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