iOS小明开发笔记(九) (tableViewCell侧滑显示多个按钮)

来源:互联网 发布:华科达软件下载 编辑:程序博客网 时间:2024/05/21 08:01


  平常中所用的tableViewCell,左滑后,编辑状态只有一个删除按钮,现在很多APP上面都自定义实现了cell侧滑自定义编辑按钮,比如qq,微信侧滑后有“消息置顶”、“标记为未读”等。 

话不多说, 代码如下:

- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath

{

   // 添加一个删除按钮

   UITableViewRowAction *deleteRowAction = [UITableViewRowActionrowActionWithStyle:UITableViewRowActionStyleDestructivetitle:@"删除"handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {

       NSLog(@"点击了删除");

        

       // 1. 更新数据

       NSMutableArray *arrModel = self.dataSource[indexPath.section];

        [arrModelremoveObjectAtIndex:indexPath.row];

       // 2. 更新UI

        [tableViewdeleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

    }];

    

   // 删除一个置顶按钮

   UITableViewRowAction *topRowAction = [UITableViewRowActionrowActionWithStyle:UITableViewRowActionStyleDefaulttitle:@"置顶"handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {

       NSLog(@"点击了置顶");

        

       // 1. 更新数据

        [self.dataSourceexchangeObjectAtIndex:indexPath.rowwithObjectAtIndex:0];

        

       // 2. 更新UI

       NSIndexPath *firstIndexPath = [NSIndexPathindexPathForRow:0 inSection:indexPath.section];

        [tableViewmoveRowAtIndexPath:indexPath toIndexPath:firstIndexPath];

    }];

    topRowAction.backgroundColor = [UIColorblueColor];

    

   // 添加一个更多按钮

   UITableViewRowAction *moreRowAction = [UITableViewRowActionrowActionWithStyle:UITableViewRowActionStyleNormaltitle:@"更多"handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {

       NSLog(@"点击了更多");

        

        [tableViewreloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationMiddle];

    }];

    moreRowAction.backgroundEffect = [UIBlurEffecteffectWithStyle:UIBlurEffectStyleDark];

    

   // 将设置好的按钮放到数组中返回

   return @[deleteRowAction, topRowAction, moreRowAction];

    

}


效果图:
0 0
原创粉丝点击