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. Scikit-learn:模型评估Model evaluation 之绘图

    http://blog.csdn.net/pipisorry/article/details/53001866 绘制ROC曲线 def plotRUC(yt, ys, title=None): ''' ...

  2. 记住经典的斐波拉契递归和阶乘递归转换为while规律

    记住经典的斐波拉契递归和阶乘递归转换为while规律.它为实现更复杂转换提供了启发性思路. # 斐波拉契--树形递归 def fab(n): if n<3: return n return fa ...

  3. 在Gazebo中使用DEM構建起伏地形環境

    所需資料下載地址: 1. https://bitbucket.org/osrf/gazebo_tutorials/raw/default/dem/files/ 数字高程模型(致謝谷歌翻譯)概述数字高程 ...

  4. JAXB(Java Architecture for XML Binding)

    marshal(Java对象转化成XML) import javax.xml.bind.annotation.XmlRootElement; //指定根元素,其他属性默认为根元素的子元素 @XmlRo ...

  5. Android开发 Jar mismatch! Fix your dependencies的问题

    有时候,当我们在导入Library的时候,会遇到Jar mismatch! Fix your dependencies这个错误.可能有如下原因: 1.两个项目的android-support-v4.j ...

  6. API创建/更新员工薪水

    DECLARE lb_inv_next_sal_date_warning BOOLEAN; lb_proposed_salary_warning BOOLEAN; lb_approved_warnin ...

  7. Android实战之ListView复选框

    项目中有用到复选框的例子,啊啊......在网上查找有关资料,大多都是过于繁琐,所以自己决定写个这个方面的demo... 先给个效果图: 在ListView中添加复选框主要注意以下几个问题: 1.Li ...

  8. Android开发学习之路--Camera之初体验

    顾名思义Camera就是拍照和录像的功能,像微信里面,我们想拍照传一下照片,就可以通过camera来拍照,然后存储照片,发送给好友.那么微信的app里面是不会直接通过camera api来实现的,因为 ...

  9. android api 镜像站

    项目地址:https://github.com/msdx/androiddoc 访问短址: http://androiddoc.qiniudn.com/

  10. 使用Python做简单的字符串匹配

    由于需要在半结构化的文本数据中提取一些特定格式的字段.数据辅助挖掘分析工作,以往都是使用Matlab工具进行结构化数据处理的建模,matlab擅长矩阵处理.结构化数据的计算,Python具有与matl ...