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