美文网首页
iOS 开发_滑动Cell出现多个可编辑按钮

iOS 开发_滑动Cell出现多个可编辑按钮

作者: iOS_PM_WEB_尛鹏 | 来源:发表于2017-03-31 17:30 被阅读0次

【作者前言】:13年入圈,分享些本人工作中遇到的点点滴滴那些事儿,17年刚开始写博客,高手勿喷!以分享交流为主,欢迎各路豪杰点评改进!

1.应用场景:

Paste_Image.png

2.实现目标:

当UITableViewCell向左滑动时,出现多个可操作的按钮

3.代码说明:

//遵循UITableViewDataSource/UITableViewDelegate协议,废话就不说了
/** 开启cell的编辑模式*
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
/** 主要实现这个代理方法即可,设置当tableView进入编辑模式时,出现的编辑按钮*/
- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        //        NSLog(@"删除被点击");
    }];
    UITableViewRowAction *editAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"编辑" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        //        NSLog(@"编辑被点击");
    }];
    UITableViewRowAction *setTopAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"置顶" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        //        NSLog(@"置顶被点击");
    }];
    editAction.backgroundColor = [UIColor grayColor];
    setTopAction.backgroundColor = [UIColor blueColor];

    return @[deleteAction, editAction, setTopAction];
}

/** 优化:设置tableView进入编辑状态时,Cell不会缩进*/
- (BOOL)tableView: (UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
    return NO;
}

相关文章

网友评论

      本文标题:iOS 开发_滑动Cell出现多个可编辑按钮

      本文链接:https://www.haomeiwen.com/subject/xlpvottx.html