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

前言: 本篇博客其实就是想介绍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. tc674div1b

    题意:给出n个孩子的初始位置,和每个孩子开始的朝向(左或者右),然后孩子的行走规则是,速度始终为1,两人相遇则两人立即转身背向而行. 现在有q次询问,每次问编号为i的孩子在时间t距离原点的距离.返回所 ...

  2. ASM:《X86汇编语言-从实模式到保护模式》第9章:实模式下中断机制和实时时钟

    中断是处理器一个非常重要的工作机制.第9章是讲中断在实模式下如何工作,第17章是讲中断在保护模式下如何工作. ★PART1:外部硬件中断 外部硬件中断是通过两个信号线引入处理器内部的,这两条线分别叫N ...

  3. C Primer Plus_第5章_运算符、表达式和语句_编程练习

    Practice 1. 输入分钟输出对应的小时和分钟. #include #define MIN_PER_H 60 int main(void) { int mins, hours, minutes; ...

  4. Huffman Tree

    哈夫曼(Huffman)树又称最优二叉树.它是一种带权路径长度最短的树,应用非常广泛. 关于Huffman Tree会涉及到下面的一些概念: 1. 路径和路径长度路径是指在树中从一个结点到另一个结点所 ...

  5. .NET LINQ 限定符操作

    限定符操作      限定符运算返回一个 Boolean 值,该值指示序列中是否有一些元素满足条件或是否所有元素都满足条件. 方法 方法名 说明 C# 查询表达式语法 Visual Basic 查询表 ...

  6. 对LR关联的一些理解

    从接触LR关联的迷茫,到现在略有感悟,小记期间的一些理解. 一开始认识关联是在LR自带学习例子,需要关联session,也知道了自动关联,手动关联以及边录制边关联. 在使用关联的过程中,也学习了web ...

  7. https

    http://www.cnblogs.com/bugly/p/5075909.html# http://www.cnblogs.com/peijian708/archive/2011/05/07/20 ...

  8. 与IE奋战的血泪史

    IE6下font-size会撑高元素,也就是说IE6下元素的最小高度为font-size的高度(蛋疼) IE6不支持两个class 例如 .a.b,类名不支持下划线开头 通过js设置样式带下划线的样式 ...

  9. 【BZOJ1180】: [CROATIAN2009]OTOCI & 2843: 极地旅行社 LCT

    竟然卡了我....忘记在push_down先下传父亲的信息了....还有splay里for():卡了我10min,但是双倍经验还是挺爽的,什么都不用改. 感觉做的全是模板题,太水啦,不能这么水了... ...

  10. 【JAVA】 UIMnager

    Java'中的几种Look and Feel 1.Metal风格 (默认) String lookAndFeel = "javax.swing.plaf.metal.MetalLookAnd ...