tableviewcell滑动显示多个按钮UITableViewRowAction(转载)
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(转载)的更多相关文章
- ios8 tableView设置滑动删除时 显示多个按钮
** * tableView:editActionsForRowAtIndexPath: //设置滑动删除时显示多个按钮 * UITableViewRowAction ...
- 微信小程序列表项滑动显示删除按钮
微信小程序并没有提供列表控件,所以也没有iOS上惯用的列表项左滑删除的功能,SO只能自己干了. 原理很简单,用2个层,上面的层显示正常的内容,下面的层显示一个删除按钮,就是记录手指滑动的距离,动态的来 ...
- duilib : 滑动显示的窗口实现以及 悬浮窗 (转载)
1. vc 判断窗口是否显示 BOOL IsWindowVisible(HWND hWnd); 2.悬浮窗 http://blog.csdn.net/lincyang/article/details ...
- iOS UITableViewCell的"滑动出现多个按钮"
本文授权转载,作者:@夏天是个大人了 前言: 本篇博客其实就是想介绍tableviewcell滑动的一些"事",昨天在逛github的时候看到的还挺有意思的三方库,简单用了一下感觉 ...
- QSplitter实现滑动窗口和悬浮按钮
1 QSplitter实现滑动窗口和悬浮按钮 软件应用中需要设计右侧滑动窗口,通过一个按钮来实现窗口的隐藏和显示,应用场景比如显示主界面的详细信息. (1) 在qt design中 ...
- Android自动更新安装后显示‘完成’‘打开’按钮
/** * 安装apk * * @param url */ private void installApk() { File apkfile = new File(apkFilePath); if ( ...
- jQuery hover事件鼠标滑过图片半透明标题文字滑动显示隐藏
1.效果及功能说明 hover事件制作产品图片鼠标滑过图片半透明,标题文字从左到右滑动动画移动显示隐藏 2.实现原理 首先把效果都隐藏,然后定义一个伪类来触发所有的效果,接下来当触发伪类后会有一个遍历 ...
- uploadify不能正确显示中文的按钮文本的解决办法
uploadify 目前不能正确显示中文的按钮文本. 我发现bug的原因是uploadify错误的使用了 js 的 escape 和 flash 的 unescape配对,而这2个是不兼容的.正确的转 ...
- Android自定义滑动显示隐藏布局
方式一:上下左右滑动显示隐藏布局 总结代码地址: http://git.oschina.net/anan9303/customView参考例子: http://www.jianshu.com/p/fc ...
随机推荐
- hdu 3006 The Number of set
二进制的状态压缩.比如A集合里面有{1,5,7}那么就表示为1010001.B集合有{3,4},二进制表示1100.A|B=1011101. 按照这样的思路 可以用01背包 把所有的组合全部求出来. ...
- November 12th 2016 Week 46th Saturday
Never love anyone who treats you like you are ordinary. 请爱那些爱你的人. Don't waste your limited energy on ...
- HTTP 错误 403.14 - Forbidden的解决办法
错误: HTTP 错误 403.14 - ForbiddenWeb 服务器被配置为不列出此目录的内容. 原因: 出现这个错误,是因为默认文档中没有增加index.aspx导致的. 解决方法: 打开 ...
- 【USACO】草地排水
Drainage Ditches 草地排水 usaco 4.2.1描述在农夫约翰的农场上,每逢下雨,Bessie最喜欢的三叶草地就积聚了一潭水.这意味着草地被水淹没了,并且小草要继续生长还要花相当长一 ...
- centOS静态ip设置
设置静态IP地址 1,先搜索了一下,得到以下解释IP P地址Netmark 子网掩码Gateway 默认网关HostName 主机名称DomainName 域 ...
- rpm 安装包制作
rpm 安装包制作 思路 参照系统自带 etcd 解压->替换掉执行文件->打包 1 预备安装工具 下载工具 yumloader #yum install -y yum-utils 解压工 ...
- Python笔记6(异常)-20160924
1. NameError 当视图访问一个未定义的变量则会发生NameError.
- ubuntu14.04下安装ice3.5.1
ubuntu 14.04下是可以通过下面这条指令安装ice3.5的 sudo apt-get install libzeroc-ice35-dev 1. 从这里下载ice源文件 主要包括两部分:ice ...
- Android KeyCode
KEYCODE_UNKNOWN=0; KEYCODE_SOFT_LEFT=1; KEYCODE_SOFT_RIGHT=2; KEYCODE_HOME=3; KEYCODE_BACK=4; KEYCOD ...
- mybatis配置sql超时时间
mybatis如果不配置,默认超时时间是不做限制的.当系统慢sql很多时,势必会增加数据库压力,系统性能及稳定性降低.所以有必要要设置sql超时设置,下面配置超时时间是5分钟. 第一步:全局配置如下 ...