iOS USES tableView to realize the right slide display selection function

  • 2020-06-15 10:20:43
  • OfStack

1. Before iOS8, we need to implement the buttons in tableview such as sliding to display delete, top, and more. The system has been written in iOS8, and only one proxy method and one class are needed

2, iOS8 protocol is correct 1 method, the return value is the array of tableview: editActionForRowAtIndexPath: method, we can write several buttons inside the method, and then put back in the array, the class of those buttons is UITableviewRowAction

3. In UITableviewRowAction class. We can style buttons to display text, background colors, and button events (implemented in block)

4, in the proxy method, we can usually put multiple buttons into the array to return, the first button into the array is displayed on the right, the last button is displayed on the left

5, if they set 1 or more buttons, the system's built-in delete button will disappear

Set tableView to edit


- (BOOL)tableView: (UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}

Usage of UITableViewRowAction:

+ (instancetype)rowActionWithStyle:(UITableViewRowActionStyle)style title:(nullable NSString *)title handler:(void (^)(UITableViewRowAction *action, NSIndexPath *indexPath))handler;

Rewrite UITableViewDelegate

- (nullable NSArray < UITableViewRowAction * > *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath

Methods.


- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row==0){
//  add 1 Three delete buttons 
deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@" delete " handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
NSLog(@" I hit delete ");
}];
}
else if (indexPath.row==1){
//  add 1 Three delete buttons 
deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@" delete " handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
NSLog(@" I hit delete ");
}];
//  add 1 Three change buttons 
moreRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@" Modify the " handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
NSLog(@" I hit Modify ");
}];
moreRowAction.backgroundEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
}
else if (indexPath.row==2){
//  add 1 Three delete buttons 
deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@" delete " handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
NSLog(@" I hit delete ");
}];
//  add 1 Three change buttons 
moreRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@" Modify the " handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
NSLog(@" I hit Modify ");
}];
moreRowAction.backgroundEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
//  add 1 Send button 
sanRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@" send " handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
NSLog(@" I hit send ");
}];
sanRowAction.backgroundColor=[UIColor orangeColor];
}
else{
//  add 1 Three delete buttons 
deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@" delete " handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
NSLog(@" I hit delete ");
}];
//  add 1 Three change buttons 
moreRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@" Modify the " handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
NSLog(@" I hit Modify ");
}];
moreRowAction.backgroundEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
//  add 1 Send button 
sanRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@" send " handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
NSLog(@" I hit send ");
}];
sanRowAction.backgroundColor=[UIColor orangeColor];
//  add 1 Send button 
OK = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"OK key " handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
NSLog(@" Click on the OK");
}];
OK.backgroundColor=[UIColor purpleColor];
}
//  Returns the set button in an array 
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];
}
return nil;
}

The above is the tableView site to introduce iOS use tableView to implement the right slide display selection function, I hope to help you.


Related articles: