How do I use uitableviewrowaction to achieve right-slip selection?
1, before iOS8, we realize tableview in the sliding display delete, top, more and so on the button, all need to achieve their own, in the IOS8 system has been written, as long as a proxy method and a class on the line
2, iOS8 the protocol to a method, the return value is an array of Tableview:editactionforrowatindexpath: method, we can write several buttons inside the method, and then put in the array to return, The class of those buttons is uitableviewrowaction.
3, in the uitableviewrowaction class. We can set the style of the button, display text, background color and button events (implemented within the block)
4, in the proxy method, we can common more than one button to return in the array, the first button to put the array is displayed on the far right, the last put in the display on the leftmost
5, if you set one or more buttons, the system comes with the Delete button disappears
Set TableView to edit
-(BOOL) TableView: (UITableView *) TableView Shouldindentwhileeditingrowatindexpath: (Nsindexpath *) Indexpath
{
return YES;
}
How to use Uitableviewrowaction:
+ (Instancetype) Rowactionwithstyle: (uitableviewrowactionstyle) style title: (Nullable NSString *) title handler: (Void ( ^) (uitableviewrowaction *action, Nsindexpath *indexpath)) handler;
Rewrite the uitableviewdelegate
-(Nullable nsarray<uitableviewrowaction *> *) TableView: (UITableView *) TableView Editactionsforrowatindexpath :(Nsindexpath *) Indexpath
Method.
-(Nsarray *) TableView: (UITableView *) TableView Editactionsforrowatindexpath: (Nsindexpath *) indexpath{if(indexpath.row==0){ //Add a Delete buttonDeleterowaction = [uitableviewrowaction rowactionwithstyle:uitableviewrowactionstyledestructive title:@"Delete"handler:^ (uitableviewrowaction *action, Nsindexpath *Indexpath) {NSLog (@"Click the Delete"); }]; } Else if(indexpath.row==1){ //Add a Delete buttonDeleterowaction = [uitableviewrowaction rowactionwithstyle:uitableviewrowactionstyledestructive title:@"Delete"handler:^ (uitableviewrowaction *action, Nsindexpath *Indexpath) {NSLog (@"Click the Delete"); }]; //Add a Modify buttonMorerowaction = [uitableviewrowaction rowactionwithstyle:uitableviewrowactionstylenormal title:@"Modify"handler:^ (uitableviewrowaction *action, Nsindexpath *Indexpath) {NSLog (@"Click the edit"); }]; Morerowaction.backgroundeffect=[Uiblureffect Effectwithstyle:uiblureffectstyledark]; } Else if(indexpath.row==2){ //Add a Delete buttonDeleterowaction = [uitableviewrowaction rowactionwithstyle:uitableviewrowactionstyledestructive title:@"Delete"handler:^ (uitableviewrowaction *action, Nsindexpath *Indexpath) {NSLog (@"Click the Delete"); }]; //Add a Modify buttonMorerowaction = [uitableviewrowaction rowactionwithstyle:uitableviewrowactionstylenormal title:@"Modify"handler:^ (uitableviewrowaction *action, Nsindexpath *Indexpath) {NSLog (@"Click the edit"); }]; Morerowaction.backgroundeffect=[Uiblureffect Effectwithstyle:uiblureffectstyledark]; //Add a Send buttonsanrowaction= [Uitableviewrowaction rowactionwithstyle:uitableviewrowactionstylenormal title:@"Send"handler:^ (uitableviewrowaction *action, Nsindexpath *Indexpath) {NSLog (@"Click the Send"); }]; Sanrowaction.backgroundcolor=[Uicolor Orangecolor]; } Else{ //Add a Delete buttonDeleterowaction = [uitableviewrowaction rowactionwithstyle:uitableviewrowactionstyledestructive title:@"Delete"handler:^ (uitableviewrowaction *action, Nsindexpath *Indexpath) {NSLog (@"Click the Delete"); }]; //Add a Modify buttonMorerowaction = [uitableviewrowaction rowactionwithstyle:uitableviewrowactionstylenormal title:@"Modify"handler:^ (uitableviewrowaction *action, Nsindexpath *Indexpath) {NSLog (@"Click the edit"); }]; Morerowaction.backgroundeffect=[Uiblureffect Effectwithstyle:uiblureffectstyledark]; //Add a Send buttonsanrowaction= [Uitableviewrowaction rowactionwithstyle:uitableviewrowactionstylenormal title:@"Send"handler:^ (uitableviewrowaction *action, Nsindexpath *Indexpath) {NSLog (@"Click the Send"); }]; Sanrowaction.backgroundcolor=[Uicolor Orangecolor]; //Add a Send buttonOK= [Uitableviewrowaction rowactionwithstyle:uitableviewrowactionstylenormal title:@"OK key"handler:^ (uitableviewrowaction *action, Nsindexpath *Indexpath) {NSLog (@"clicked OK."); }]; Ok.backgroundcolor=[Uicolor Purplecolor]; } //put the Set button in the array to return if(indexpath.row==0) { return@[deleterowaction]; }Else if(indexpath.row==1){ return@[deleterowaction,morerowaction]; }Else if(indexpath.row==2){ return@[deleterowaction,morerowaction,sanrowaction]; }Else if(indexpath.row==3){ return@[deleterowaction,morerowaction,sanrowaction,ok]; } returnNil;}
IOS TableView Right Slide display selection