iOS > = 5.0使用第三方效果图

iOS> = 8.0使用系统方法效果图

MGSwipeTableCell(Github上的三方库)- iOS >= 5.0

直接使用比较简单 通过代码看一下



首先签这个协议MGSwipeTableCellDelegate

添加左边按钮方法
- (NSArray *)btnLeftCount:(int)count
{
    NSMutableArray *result = [NSMutableArray array];
    UIColor *colors[3] = {[UIColor greenColor],
        [UIColor colorWithRed:0 green:0x99/255.0 blue:0xcc/255.0 alpha:1.0],
        [UIColor colorWithRed:0.59 green:0.29 blue:0.08 alpha:1.0]};;
    for (int i = 0; i < count; i ++) {
        // 按钮提供了几个方法, 可以点进去看一看
        MGSwipeButton *btn = [MGSwipeButton buttonWithTitle:@"" backgroundColor:colors[i] padding:15
callback:^BOOL(MGSwipeTableCell *sender) {
            return YES;
        }];
        // 把按钮加到数组中
        [result addObject:btn];
    }
    return result;
}
添加右边按钮的方法
- (NSArray *)btnRightCount:(int)count
{
    NSMutableArray *result = [NSMutableArray array];
    NSArray *titleArray = @[@"删除", @"标记未读"];
    UIColor *color[2] = {[UIColor redColor], [UIColor lightGrayColor]};
    for (int i = 0; i < count; i ++) {
        MGSwipeButton *btn = [MGSwipeButton buttonWithTitle:titleArray[i] backgroundColor:color[i] padding:15
callback:^BOOL(MGSwipeTableCell *sender) {
            BOOL autoHide = i != 0;
            return autoHide;
        }];
        // 把按钮加到数组中
        [result addObject:btn];
    }
    return result;
}
重用池可以这样写
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellID = @"cellId";
    // 这里如果MGSwipeTableCell是足够你使用的, 你可以直接使用
    // 或者自定义创建cell继承于MGSwipeTableCell, 像我下面代码这样
    XTCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[XTCustomCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
    }
    cell.label.text = [NSString stringWithFormat:@"%ld------%@", (long)indexPath.row, self.arrayTest[indexPath.row]];
    cell.label.font = [UIFont systemFontOfSize:20];
    // 指定代理人
    cell.delegate = self;
    // NO: 只有单个可以滑动 , YES: 多个可以滑动
    cell.allowsMultipleSwipe = NO;
    return cell;
}
添加按钮
-(NSArray*) swipeTableCell:(MGSwipeTableCell*) cell swipeButtonsForDirection:(MGSwipeDirection)direction
             swipeSettings:(MGSwipeSettings*) swipeSettings expansionSettings:(MGSwipeExpansionSettings*) expansionSettings;
{
    if (direction == MGSwipeDirectionRightToLeft) {

        return [self btnRightCount:2];
    }
    else {
        return [self btnLeftCount:3];

    }
}
按钮的点击代理方法
-(BOOL) swipeTableCell:(MGSwipeTableCell*) cell tappedButtonAtIndex:(NSInteger) index direction:(MGSwipeDirection)direction
fromExpansion:(BOOL) fromExpansion
{

    switch (direction) {
        case MGSwipeDirectionLeftToRight: {
            if (index == 0) {
                NSLog(@"right ------- 0");
            }else{
                NSLog(@"right ------- 1");
            }
            break;
        }
        case MGSwipeDirectionRightToLeft: {
            if (index == 0) {
                NSLog(@"left ------- 0");
                // 这里简单的做了个删除操作
                NSIndexPath * path = [_tableView indexPathForCell:cell];
                [_arrayTest removeObjectAtIndex:path.row];
                [_tableView deleteRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationLeft];
                return NO;
            }else{
                NSLog(@"left ------- 1");
            }
            break;
        }
    }
    return YES;
}

iOS8 之后也提供了类似的实现

- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive
title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        [self.arrayTest removeObjectAtIndex:indexPath.row];
        [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    }];

    UITableViewRowAction *topRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"置顶"
handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        [self.arrayTest exchangeObjectAtIndex:indexPath.row withObjectAtIndex:0];
        NSIndexPath *firstIndexPath = [NSIndexPath indexPathForRow:0 inSection:indexPath.section];
        [tableView moveRowAtIndexPath:indexPath toIndexPath:firstIndexPath];
    }];
    topRowAction.backgroundColor = [UIColor blueColor];

    UITableViewRowAction *moreRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"更多"
 handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationMiddle];
    }];
    return @[deleteRowAction,topRowAction,moreRowAction];
}

滑动UITableViewCell出现多个按钮的更多相关文章

  1. UITableView左右滑动cell无法显示“删除”按钮的原因分析

    http://www.cocoachina.com/bbs/read.php?tid-145693.html - (void)tableView:(UITableView *)tableView co ...

  2. 自定义UITableViewCell上的delete按钮

    http://blog.csdn.net/xiaoxuan415315/article/details/7834940

  3. 自定义UITableViewCell 的delete按钮

    自定义UITableViewCell上的delete按钮 滑动列表行(UITableViewCell)出现删除按钮时,默认是英文“delete”,这份代码片段能够将“delete”变成中文”删除“,甚 ...

  4. [Swift通天遁地]二、表格表单-(7)电子邮件Mail:实现单元格左右滑动调出功能按钮

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  5. UITableViewCell上的按钮点击事件处理

    转自:  http://www.aichengxu.com/view/42871 UITableViewCell上的按钮点击事件处理,有需要的朋友可以参考下. 今天突然做项目的时候,又遇到处理自定义的 ...

  6. tableviewcell滑动显示多个按钮UITableViewRowAction(转载)

    demo截图 ios8 新的属性 typedef NS_ENUM(NSInteger, UITableViewRowActionStyle) { UITableViewRowActionStyleDe ...

  7. QSplitter实现滑动窗口和悬浮按钮

    1         QSplitter实现滑动窗口和悬浮按钮 软件应用中需要设计右侧滑动窗口,通过一个按钮来实现窗口的隐藏和显示,应用场景比如显示主界面的详细信息. (1)   在qt design中 ...

  8. 写一个js向左滑动删除 交互特效的插件——Html5 touchmove

    需求描述 需要实现类似QQ中对联系人的操作:向左滑动,滑出删除按钮.滑动超过一半时松开则自动滑到底,不到一半时松开则返回原处. 纯js实现 使用了h5的touchmove等事件,以及用js动态改变cs ...

  9. 自定义listView添加滑动删除功能

    今天研究了一下android里面的手势,结合昨天学习的自定义View,做了一个自定义的listview,继承自listView,添加了条目的滑动手势操作,滑动后出现一个删除按钮,点击删除按钮,触发一个 ...

随机推荐

  1. django-redis

    linuxapt-get install redis-serverpip install django-redis vim /etc/redis/redis.conf maxmemory 20mb s ...

  2. Linux中查看文本文件内容命令cat/tac/nl/more/less/head/tail/vi总结

    概述 在Linux系统下,有很多命令可以查看文本文件的内容,如cat/tac/nl/more/less/head/tail等命令,当然还有vi/nano等文本编辑器.在这里,我只介绍其中自己常用的一部 ...

  3. nginx中configure脚本支持的常用选项,拍摄自《Nginx高性能Web服务器详解》

  4. BeanUtils Exception 之 FastHashMap

    这里仅仅是为了记录一件十分奇怪的事情,在使用BeanUtils的过程中,所有的依赖包都添加了, common logging common collections ··· 在为boolean 这种基本 ...

  5. Latex居中

    居中文本 环境:\begin{center} 第一行\\第二行\\...第n行 \end{center}.可以用\\[长度]来插入可以省略的额外行间距.在一个环境内部,可以用命令\centering来 ...

  6. Ubuntu 16.04 安装和使用QQ最简洁的方式

    推荐参考网址: 1  http://www.ubuntukylin.com/ 2  http://www.ubuntukylin.com/application/ Wine QQ 1  http:// ...

  7. 4.1、Android Stuido配置你的Build Variant

    每个版本的build variant代表了你可以构建的每一个版本.虽然你未直接配置build variants,你可以通过配置build type和product flavor. 比如,一个demo的 ...

  8. Android必知必会-Stetho调试工具

    一.背景 Stetho是 Facebook 出品的一个强大的 Android 调试工具,使用该工具你可以在 Chrome Developer Tools查看APP的布局, 网络请求(仅限使用Volle ...

  9. SSH深度历险(十) AOP原理及相关概念学习+AspectJ注解方式配置spring AOP

    AOP(Aspect Oriented Programming),是面向切面编程的技术.AOP基于IoC基础,是对OOP的有益补充. AOP之所以能得到广泛应用,主要是因为它将应用系统拆分分了2个部分 ...

  10. Android开发学习之路--Activity之四种启动模式

    后天终于可以回家了,马上就要过年了,趁着年底打酱油的模式,就多学习学习,然后记录记录吧.关于Activity已经学习了七七八八了,还有就是Activity的四种启动模式了,它们分别为,standard ...