IOS TableView Right Slide display selection

Source: Internet
Author: User

IOS TableView Right Slide display selection 

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

1234 - (BOOL)tableView: (UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath{    returnYES;}

How to use Uitableviewrowaction:

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

Rewrite the uitableviewdelegate

1 - (nullable NSArray<uitableviewrowaction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath</uitableviewrowaction *>

Method.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 666768697071727374757677787980818283848586878889 - (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{        if(indexPath.row==0)    {                // 添加一个删除按钮        deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除"handler:^(UITableViewRowAction *action, NSIndexPath *indexPath)         {            NSLog(@"点击了删除");         }];    }        elseif(indexPath.row==1)    {               // 添加一个删除按钮        deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除"handler:^(UITableViewRowAction *action, NSIndexPath *indexPath)        {            NSLog(@"点击了删除");        }];                // 添加一个修改按钮        moreRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"修改"handler:^(UITableViewRowAction *action, NSIndexPath *indexPath)         {            NSLog(@"点击了修改");        }];        moreRowAction.backgroundEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];            }        elseif(indexPath.row==2)    {                // 添加一个删除按钮        deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除"handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {            NSLog(@"点击了删除");                     }];                // 添加一个修改按钮        moreRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"修改"handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {            NSLog(@"点击了修改");        }];        moreRowAction.backgroundEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];                // 添加一个发送按钮                sanRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"发送"handler:^(UITableViewRowAction *action, NSIndexPath *indexPath)         {            NSLog(@"点击了发送");        }];        sanRowAction.backgroundColor=[UIColor orangeColor];     }    else    {                // 添加一个删除按钮        deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除"handler:^(UITableViewRowAction *action, NSIndexPath *indexPath)         {            NSLog(@"点击了删除");        }];                // 添加一个修改按钮        moreRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"修改"handler:^(UITableViewRowAction *action, NSIndexPath *indexPath)         {            NSLog(@"点击了修改");        }];        moreRowAction.backgroundEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];                // 添加一个发送按钮               sanRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"发送"handler:^(UITableViewRowAction *action, NSIndexPath *indexPath)         {            NSLog(@"点击了发送");        }];        sanRowAction.backgroundColor=[UIColor orangeColor];        // 添加一个发送按钮                OK = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"OK键"handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {            NSLog(@"点击了OK");        }];        OK.backgroundColor=[UIColor purpleColor];    }             // 将设置好的按钮放到数组中返回        if(indexPath.row==0)     {                return@[deleteRowAction];    }    else if(indexPath.row==1)    {                return@[deleteRowAction,moreRowAction];    }    elseif(indexPath.row==2)    {                return@[deleteRowAction,moreRowAction,sanRowAction];    }elseif(indexPath.row==3)    {                    return @[deleteRowAction,moreRowAction,sanRowAction,OK];      }        returnnil;}

IOS TableView Right Slide display selection

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.