demo截图

ios8 新的属性

typedef NS_ENUM(NSInteger, UITableViewRowActionStyle) {

UITableViewRowActionStyleDefault = 0,

UITableViewRowActionStyleDestructive = UITableViewRowActionStyleDefault,

UITableViewRowActionStyleNormal

} NS_ENUM_AVAILABLE_IOS(8_0);

NS_CLASS_AVAILABLE_IOS(8_0)
@interface UITableViewRowAction : NSObject <NSCopying>

+ (instancetype)rowActionWithStyle:(UITableViewRowActionStyle)style title:(NSString *)title handler:(void (^)(UITableViewRowAction
*action, NSIndexPath *indexPath))handler;

/**

* tableview:editActionsForRowAtIndexPath:// 设置滑动时显示多个按钮

* UITableviewRowAction                           //通过此类创建按钮

1、在ios8以前,我们实现tableview中滑动显示删除,置顶,更多等等的按钮时,都需要自己去实现,在ios8中系统已经写好了,只要一个代理方法和一个类就行了

2、ios8的协议对了一个方法,返回值是数组的tableview:editActionForRowAtIndexPath:方法,我们可以在方法内部写好几个按钮,然后放到数组中返回,那些按钮的类就是UITableviewRowAction

3、在UITableviewRowAction类。我们可以设置按钮的样式,显示文字、背景色和按钮事件(在block内实现)

4、在代理方法中,我们可以常见多个按钮放到数组中返回,最先放入数组的按钮显示在最右边,最后放入的显示在最左边

5、如果自己设定一个或多个按钮,系统自带的删除按钮就消失了

/////////////////下面实现相关代码////////////////////////////

(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{

return
YES;

}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

{

return 
UITableViewCellEditingStyleDelete;

}

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath
*)indexPath{

if (editingStyle ==
UITableViewCellEditingStyleDelete) {

[self.dataSource
removeObjectAtIndex:indexPath.row];

[self.tableView
deleteRowsAtIndexPaths:@[indexPath]
withRowAnimation:UITableViewRowAnimationAutomatic];

}

}

- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{

//设置删除按钮

UITableViewRowAction *deleteRowAction = [UITableViewRowAction
rowActionWithStyle:UITableViewRowActionStyleDestructive
title:@"删除"handler:^(UITableViewRowAction *action,NSIndexPath *indexPath) {

[self.dataSource
removeObjectAtIndex:indexPath.row];

[self.tableView
deleteRowsAtIndexPaths:@[indexPath]
withRowAnimation:UITableViewRowAnimationAutomatic];

}];

//设置收藏按钮

UITableViewRowAction *collectRowAction = [UITableViewRowAction
rowActionWithStyle:UITableViewRowActionStyleDefault
title:@"收藏"handler:^(UITableViewRowAction *action,NSIndexPath *indexPath) {

collectRowAction.backgroundColor = [UIColor
greenColor];

UIAlertView *alertView = [[UIAlertView
alloc]initWithTitle:@"收藏"
message:@"收藏成功"
delegate:self
cancelButtonTitle:@"确定"
otherButtonTitles:nil,
nil];

[alertView show];

}];

//设置置顶按钮

UITableViewRowAction *topRowAction = [UITableViewRowAction
rowActionWithStyle:UITableViewRowActionStyleDefault
title:@"置顶"
handler:^(UITableViewRowAction *action,
NSIndexPath *indexPath) {

[self.dataSource
exchangeObjectAtIndex:indexPath.row
withObjectAtIndex:0];

NSIndexPath *firstIndexPath = [NSIndexPath
indexPathForRow:0
inSection:indexPath.section];

[tableView moveRowAtIndexPath:indexPath
toIndexPath:firstIndexPath];

}];

collectRowAction.backgroundEffect = [UIBlurEffect
effectWithStyle:UIBlurEffectStyleLight];

topRowAction.backgroundColor = [UIColor
blueColor];

collectRowAction.backgroundColor = [UIColor
grayColor];

return 
@[deleteRowAction,collectRowAction,topRowAction];

}

下面是swift的写法

override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]? {

let topAction = UITableViewRowAction(style: .Default, title: "置顶") {

(action: UITableViewRowAction!, indexPath: NSIndexPath!) -> Void in

tableView.editing = false

}

return [topAction]

}

在这里可以添加任意多个操作。要确保这个代码生效,还是需要实现commitEditingStyle这个delegate方法,哪怕里面什么也不处理:

override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {

}

tableviewcell滑动显示多个按钮UITableViewRowAction(转载)的更多相关文章

  1. ios8 tableView设置滑动删除时 显示多个按钮

      ** *  tableView:editActionsForRowAtIndexPath:     //设置滑动删除时显示多个按钮 *  UITableViewRowAction          ...

  2. 微信小程序列表项滑动显示删除按钮

    微信小程序并没有提供列表控件,所以也没有iOS上惯用的列表项左滑删除的功能,SO只能自己干了. 原理很简单,用2个层,上面的层显示正常的内容,下面的层显示一个删除按钮,就是记录手指滑动的距离,动态的来 ...

  3. duilib : 滑动显示的窗口实现以及 悬浮窗 (转载)

    1. vc 判断窗口是否显示  BOOL IsWindowVisible(HWND hWnd); 2.悬浮窗 http://blog.csdn.net/lincyang/article/details ...

  4. iOS UITableViewCell的"滑动出现多个按钮"

    本文授权转载,作者:@夏天是个大人了 前言: 本篇博客其实就是想介绍tableviewcell滑动的一些"事",昨天在逛github的时候看到的还挺有意思的三方库,简单用了一下感觉 ...

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

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

  6. Android自动更新安装后显示‘完成’‘打开’按钮

    /** * 安装apk * * @param url */ private void installApk() { File apkfile = new File(apkFilePath); if ( ...

  7. jQuery hover事件鼠标滑过图片半透明标题文字滑动显示隐藏

    1.效果及功能说明 hover事件制作产品图片鼠标滑过图片半透明,标题文字从左到右滑动动画移动显示隐藏 2.实现原理 首先把效果都隐藏,然后定义一个伪类来触发所有的效果,接下来当触发伪类后会有一个遍历 ...

  8. uploadify不能正确显示中文的按钮文本的解决办法

    uploadify 目前不能正确显示中文的按钮文本. 我发现bug的原因是uploadify错误的使用了 js 的 escape 和 flash 的 unescape配对,而这2个是不兼容的.正确的转 ...

  9. Android自定义滑动显示隐藏布局

    方式一:上下左右滑动显示隐藏布局 总结代码地址: http://git.oschina.net/anan9303/customView参考例子: http://www.jianshu.com/p/fc ...

随机推荐

  1. 转:iOS 屏幕适配,autoResizing autoLayout和sizeClass图文详解

    1. autoResizing autoresizing是苹果早期的ui布局适配的解决办法,iOS6之前完全可以胜任了,因为苹果手机只有3.5寸的屏幕,在加上手机app很少支持横屏,所以iOS开发者基 ...

  2. 面试题-Java基础-异常部分

    1.Java中的两种异常类型是什么?他们有什么区别? Java中有两种异常:受检查的(checked)异常和不受检查的(unchecked)异常.不受检查的异常不需要在方法或者是构造函数上声明,就算方 ...

  3. js常用语句写法

    1.for语句 for(var i = 0; i<6; i++) //0,1,2,3,4,5

  4. IIS优化服务器性能导致QuartZ任务未运行

    问题: IIS 为优化服务器性能,会自动对它认为休眠的应用程序进行资源回收,资源回收将会导致网站应用程序关闭. 解决方案: 1.  设置闲置超时为0,固定回收时间间隔为0,即IIS不主动回收闲置进程 ...

  5. in_array 判断的一些见解

    我个人见解in_array的判断是== 并不是===  证明如下: $arr=(array_merge(range(1, 9),range('a', 'z'),range('A', 'Z')));$m ...

  6. python 小程序(1)

    这是在一个文件夹下将符合条件的文件中,第一列是相同的,对每个文件中第二列的数求和,输出为一个txt文件,文件内容格式为:第一列的每个数对应[这个所有符合条件的文件中第一列这个数对应的第二个数的]求和 ...

  7. C-Free 5.0编译失败问题解决办法

    解决关于C-Free 5.0编译时提示错误:[Error] undefined reference to `__dyn_tls_init_callback' 解决办法: 因为错误提示的路径是C:\Mi ...

  8. ArcGIS导出辖区边界点坐标

    1.使用ArcGIS打开.mxd地图文件 2.选择[Geoprocessing][ArcToolbox]  3.展开菜单,选中如图所示菜单,双击打开 4.选择对应的辖区节点   5.点击OK后,会在左 ...

  9. Solr搜索技术

    Solr搜索技术 今日大纲 回顾上一天的内容: 倒排索引 lucene和solr的关系 lucene api的使用 CRUD 文档.字段.目录对象(类).索引写入器类.索引写入器配置类.IK分词器 查 ...

  10. hdu1020

    #include <stdio.h> int main(void){ int n,i,c; char txt[10001]; scanf("%d", &n); ...