iOS开发之cell多按钮

iOS开发之cell多按钮

iOS开发经常出现cell需要多个按钮,一般以为要导入第三方框架。但其实iOS 8以后,系统提供了UITableViewRowAction以及新的delegate方法,使得自定义一些操作变得非常容易。诸如置顶,删除,修改,更多,收藏等按钮。

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

// 添加一个置顶按钮

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

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

        // 1. 更新数据

        [_diarary exchangeObjectAtIndex:indexPath.row withObjectAtIndex:0];

        // 2. 更新UI

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

        [tableView moveRowAtIndexPath:indexPath toIndexPath:firstIndexPath];

    }];

 topRowAction.backgroundColor = [UIColor redColor];//可自定义按钮颜色

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

    NSLog(@"delete");

}];

 return @[topRowAction,deleteRowAction];

}

由此可添加任意多个按钮。要确保这个代码生效,还需要实现commitEditingStyle这个delegate方法,哪怕里面什么也不写。

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

}

 切记:添加多组按钮后,系统自带的删除按钮将不存在,需自己重写。

 
  • 相关阅读:
    Emmet Documentation
    微软雅黑的Unicode码和英文名
    Eclipse让代码自动换行(WordWarp)
    如何更改MyEclipse代码自动换行的长度?
    PHP之道推荐使用PHP版本,数据库方式,以及虚拟机的创建程序
    ime-mode:disabled是什么?
    document.getElementsByClassName在ie8及其以下浏览器的兼容性问题
    Unobtrusive JavaScript 不唐突的JavaScript的七条准则
    详解 arguments, callee, caller, call, apply的区别
    怎样在谷歌浏览器上加载金山词霸的取词插件?
  • 原文地址:https://www.cnblogs.com/wniruoanhao/p/5024228.html
  •  topRowAction.backgroundColor = [UIColor redColor];//可自定义按钮颜色

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

        NSLog(@"delete");

    }];

     return @[topRowAction,deleteRowAction];

    }

    由此可添加任意多个按钮。要确保这个代码生效,还需要实现commitEditingStyle这个delegate方法,哪怕里面什么也不写。

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

    }

     切记:添加多组按钮后,系统自带的删除按钮将不存在,需自己重写。