zoukankan      html  css  js  c++  java
  • tableviewcell滑动显示多个按钮UITableViewRowAction(转载)

    demo截图

    ios8 新的属性

    typedef NS_ENUM(NSInteger, UITableViewRowActionStyle) {

        UITableViewRowActionStyleDefault = 0,

        UITableViewRowActionStyleDestructive = UITableViewRowActionStyleDefault,

        UITableViewRowActionStyleNormal

    } NS_ENUM_AVAILABLE_IOS(8_0);


    NS_CLASS_AVAILABLE_IOS(8_0) @interface UITableViewRowAction : NSObject <NSCopying>


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

    /**

    * tableview:editActionsForRowAtIndexPath:// 设置滑动时显示多个按钮

    * UITableviewRowAction                           //通过此类创建按钮

    1、在ios8以前,我们实现tableview中滑动显示删除,置顶,更多等等的按钮时,都需要自己去实现,在ios8中系统已经写好了,只要一个代理方法和一个类就行了

    2、ios8的协议对了一个方法,返回值是数组的tableview:editActionForRowAtIndexPath:方法,我们可以在方法内部写好几个按钮,然后放到数组中返回,那些按钮的类就是UITableviewRowAction

    3、在UITableviewRowAction类。我们可以设置按钮的样式,显示文字、背景色和按钮事件(在block内实现)

    4、在代理方法中,我们可以常见多个按钮放到数组中返回,最先放入数组的按钮显示在最右边,最后放入的显示在最左边

    5、如果自己设定一个或多个按钮,系统自带的删除按钮就消失了

    /////////////////下面实现相关代码////////////////////////////

    (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{

        return YES;

    }

    - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

    {

        return  UITableViewCellEditingStyleDelete;  

    }

    -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{ 

        if (editingStyle == UITableViewCellEditingStyleDelete) {

            [self.dataSource removeObjectAtIndex:indexPath.row];

            [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

        }

    }

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

        //设置删除按钮

          UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除"handler:^(UITableViewRowAction *action,NSIndexPath *indexPath) {

              [self.dataSource removeObjectAtIndex:indexPath.row];

              [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

          }];

        //设置收藏按钮

        UITableViewRowAction *collectRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"收藏"handler:^(UITableViewRowAction *action,NSIndexPath *indexPath) {

            collectRowAction.backgroundColor = [UIColor greenColor];

            UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"收藏" message:@"收藏成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];

            [alertView show];

        }];

        //设置置顶按钮

        UITableViewRowAction *topRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"置顶" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {

            [self.dataSource exchangeObjectAtIndex:indexPath.row withObjectAtIndex:0];

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

            [tableView moveRowAtIndexPath:indexPath toIndexPath:firstIndexPath];

        }];

        collectRowAction.backgroundEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];

        topRowAction.backgroundColor = [UIColor blueColor];

        collectRowAction.backgroundColor = [UIColor grayColor];

        return  @[deleteRowAction,collectRowAction,topRowAction];

    }

     

    下面是swift的写法

    override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]? {

        let topAction = UITableViewRowAction(style: .Default, title: "置顶") {

            (action: UITableViewRowAction!, indexPath: NSIndexPath!) -> Void in

            tableView.editing = false

        }

        return [topAction]

    }

    在这里可以添加任意多个操作。要确保这个代码生效,还是需要实现commitEditingStyle这个delegate方法,哪怕里面什么也不处理: 

    override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {

     

    }

    握不住的沙,干脆扬了它。
  • 相关阅读:
    [Jenkins] 批量删除构建历史
    [Android Tips] 32. 解决 Android Device Monitor 在 Mac OS X 卡住
    [Gradle] 查看项目依赖
    [Gradle] 获取 gradle 命令行参数
    [Android Tips] 31.如何将第三库引入的 Permission 删除掉
    [Gradle] 发布构件到本地仓库
    [Android Tips] 30.如何在 Android Studio 中一次性格式化所有代码
    下载安卓应用的历史版本
    设置int、float型数据的输出格式
    头文件limits—各个类型的数据的范围
  • 原文地址:https://www.cnblogs.com/zj901203/p/4606804.html
Copyright © 2011-2022 走看看