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.