本文授权转载,作者:@夏天是个大人了

前言: 本篇博客其实就是想介绍tableviewcell滑动的一些"事",昨天在逛github的时候看到的还挺有意思的三方库,简单用了一下感觉不错,一作为记录,二是希望有类似需求的可以得到帮助。

本篇介绍了iOS 5之后(使用三方库) iOS 8之后(系统方法)分别的实现方式

效果图 - ios>= 5.0

效果图 - ios>= 8.0

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

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

首先签这个协议MGSwipeTableCellDelegate

添加左边按钮方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
- (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;
}

添加右边按钮的方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
- (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;
}

重用池可以这样写

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
- (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;
}

添加按钮

1
2
3
4
5
6
7
8
9
10
-(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];
    }
}

按钮的点击代理方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
-(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;
}

iOS 8之后也提供了类似的实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
- (NSArray<uitableviewrowaction *> *)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];

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

  1. iOS系列 基础篇 06 标签和按钮 (Label & Button)

    iOS系列 基础篇 06 标签和按钮 (Label & Button) 目录: 标签控件 按钮控件 小结 标签和按钮是两个常用的控件,下面咱们逐一学习. 1. 标签控件 使用Single Vi ...

  2. 纯css隐藏移动端滚动条解决方案(ios上流畅滑动)

    纯css隐藏移动端滚动条解决方案(ios上流畅滑动) html代码展示(直接复制代码保存至本地文件运行即可): <!DOCTYPE html> <html lang="en ...

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

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

  4. iOS点击获取短信验证码按钮

    概述 iOS点击获取短信验证码按钮, 由于 Demo整体测试运行效果 , 整个修改密码界面都已展现, 并附送正则表达式及修改密码逻辑. 详细 代码下载:http://www.demodashi.com ...

  5. iOS UITableViewCell UITableVIewController 纯代码开发

    iOS UITableViewCell UITableVIewController 纯代码开发 <原创> .纯代码 自定义UITableViewCell 直接上代码 ////// #imp ...

  6. 纯css隐藏移动端滚动条解决方案(ios上流畅滑动)---转载

    html代码展示(直接复制代码保存至本地文件运行即可): <!DOCTYPE html> <html lang="en"> <head> < ...

  7. iOS UITableViewCell滑动删除

    一般我们使用列表的形式展现数据就会用到UITableView.在熟练掌握了用UITableView展示数据以后,开发过程中可能会遇到需要删除数据的需求,我们想实现在一行数据上划动一下,然后出现一个删除 ...

  8. iOS 在tableView上添加button导致按钮没有点击效果和不能滑动的 zhuang

    转载请注明出处. 今天在调试代码的时候,在tableviewcell上添加button,发现button快速点击的话,是看不出点击效果的,查找资料发现, ios7上UITableViewCell子层容 ...

  9. iOS手势(滑动)返回的实现(自定义返回按钮)

    如果各位使用的是storyboard布局的话,且用的是系统的返回按钮,那么是自动会有滑动返回效果的,但是相信各位做项目的,一般都是用的自定义的返回按钮,所以我贴几行代码,看看怎么实现系统自带的滑动返回 ...

随机推荐

  1. winfrom 隐藏任务栏(win7)

    1:新建winfrom 窗体应用程序 2:拖入contextMenuStrip.notifyIcon 2个控件 3:如图 4:code: 注意:复制控件事件要注册 using System; usin ...

  2. 跟随Rodolfo进入VR时代!

    大家好,我是Rodolfo!一个热衷于Virtual Reality (VR),Operating System,Flushbonading和Algorithm的IT行业CTO. 基于多年的自我喜好研 ...

  3. Linux使用ssh-keygen实现SSH无密码登录

    一.原理简介: 1.SSH公钥认证的基本原理:     SSH是一个专为远程登录会话和其他网络服务提供安全性的协议.默认状态下SSH链接是需要密码认证的,可以通过添加系统认证(即公钥-私钥)的修改,修 ...

  4. iOS- 制作U盘安装Mac OS X 系统

    需要: 1.下载Yomitate.app or Capitan.app 跳转到官网或者第三方. 2.准备一台Mac 电脑 3.准备一个8G U盘. 安装: 步骤1.将Yomitate.app 拖到应用 ...

  5. 犀利的报表系统,发票据与报表开发的快速利器,AgileEAS.NET SOA中间件GReport使用指南

    一.前言 AgileEAS.NET SOA 中间件平台是一款基于基于敏捷并行开发思想和Microsoft .Net构件(组件)开发技术而构建的一个快速开发应用平台.用于帮助中小型软件企业建立一条适合市 ...

  6. java DMO及增删改查代码的自动生成

    在web开发过程中,尤其是后台管理系统的开发中,少不了增删改成的基础操作,原来我自己的做法是一份一份的拷贝粘贴,然后修改其中的不同,然而这样既枯燥无味又浪费了大量的时间,所以根据自己项目结构的特点写了 ...

  7. Struts2 回顾总结

    1.第一个Struts2入门案例 1.找jar包 Struts2-core xwork-core ognl javasist freemarker commons-lang commons-io co ...

  8. js文件上传

    DOM: <form id="clueForm" class="insert-dialog" action="/xxx/xxx"met ...

  9. iOS通知的使用

    注册:[[NSNotificationCenter defaultCenter] postNotificationName:@"changeColor" object:self]; ...

  10. Spring Boot下配置MyBatis多数据源

    http://m.blog.csdn.net/article/details?id=51481911