滑动UITableViewCell出现多个按钮
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出现多个按钮的更多相关文章
- UITableView左右滑动cell无法显示“删除”按钮的原因分析
http://www.cocoachina.com/bbs/read.php?tid-145693.html - (void)tableView:(UITableView *)tableView co ...
- 自定义UITableViewCell上的delete按钮
http://blog.csdn.net/xiaoxuan415315/article/details/7834940
- 自定义UITableViewCell 的delete按钮
自定义UITableViewCell上的delete按钮 滑动列表行(UITableViewCell)出现删除按钮时,默认是英文“delete”,这份代码片段能够将“delete”变成中文”删除“,甚 ...
- [Swift通天遁地]二、表格表单-(7)电子邮件Mail:实现单元格左右滑动调出功能按钮
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- UITableViewCell上的按钮点击事件处理
转自: http://www.aichengxu.com/view/42871 UITableViewCell上的按钮点击事件处理,有需要的朋友可以参考下. 今天突然做项目的时候,又遇到处理自定义的 ...
- tableviewcell滑动显示多个按钮UITableViewRowAction(转载)
demo截图 ios8 新的属性 typedef NS_ENUM(NSInteger, UITableViewRowActionStyle) { UITableViewRowActionStyleDe ...
- QSplitter实现滑动窗口和悬浮按钮
1 QSplitter实现滑动窗口和悬浮按钮 软件应用中需要设计右侧滑动窗口,通过一个按钮来实现窗口的隐藏和显示,应用场景比如显示主界面的详细信息. (1) 在qt design中 ...
- 写一个js向左滑动删除 交互特效的插件——Html5 touchmove
需求描述 需要实现类似QQ中对联系人的操作:向左滑动,滑出删除按钮.滑动超过一半时松开则自动滑到底,不到一半时松开则返回原处. 纯js实现 使用了h5的touchmove等事件,以及用js动态改变cs ...
- 自定义listView添加滑动删除功能
今天研究了一下android里面的手势,结合昨天学习的自定义View,做了一个自定义的listview,继承自listView,添加了条目的滑动手势操作,滑动后出现一个删除按钮,点击删除按钮,触发一个 ...
随机推荐
- android 自定义ViewGroup之浪漫求婚
*本篇文章已授权微信公众号 guolin_blog (郭霖)独家发布 1.最终效果 有木有发现还是很小清新的感觉 2.看整体效果这是一个scrollView,滑动时每个子view都有一个或多个动画效果 ...
- springMVC源码分析--SimpleServletHandlerAdapter(二)
上一篇博客springMVC源码分析--HandlerAdapter(一)中我们主要介绍了一下HandlerAdapter接口相关的内容,实现类及其在DispatcherServlet中执行的顺序,接 ...
- Dynamics CRM2016 Web API之创建记录
前篇介绍了通过primary key来查询记录,那query的知识点里面还有很多需要学习的,这个有待后面挖掘,本篇来简单介绍下用web api的创建记录. 直接上代码,这里的entity的属性我列了几 ...
- Makefile常用函数总结
在Makefile中可以使用函数来处理变量,从而让我们的命令或是规则更为的灵活和具 有智能.make所支持的函数也不算很多,不过已经足够我们的操作了.函数调用后,函 数的返回值可以当做变量来使用. 一 ...
- windows 7、8分区
如果你的机器一开始安装的是windows7或者8, 一般分配的分区都是主分区.如果你想再搭配个linux操作系统,搞个双系统啥的,可能总是失败.我有血的教训啊. 从源头上可以解决分区问题,就是可以在安 ...
- FFmpeg源代码简单分析:av_write_trailer()
===================================================== FFmpeg的库函数源代码分析文章列表: [架构图] FFmpeg源代码结构图 - 解码 F ...
- 02_MyBatis项目结构,所需jar包,ehcache.xml配置,log4j.properties,sqlMapConfig.xml配置,SqlMapGenerator.xml配置
项目结构(所需jar包,配置文件) sqlMapConfig.xml的配置内容如下: <?xmlversion="1.0"encoding="UTF-8&qu ...
- android 使用Vysor投影到电脑
有没有好的投影软件可以将android屏幕投影到电脑,当然这种很多,比如360就自带了投影功能,小米盒子也可以(不过貌似只能支持到4.4版本),今天要说的是Vysor,google的一款投影软件. V ...
- android AlarmManager讲解
Android系统闹钟定时功能框架,总体来说就是用数据库存储定时数据,有一个状态管理器来统一管理这些定时状态的触发和更新.在Andriod系统中实现定时功能,最终还是要用到系统提供的AlarmMana ...
- Hibernate进阶知识点必备
hibernate.cfg.xml的常用的配置 hibernate.show_sql:是否把Hibernate运行时的SQL语句输出到控制台,编码阶段便于测试,为true的好 -hibernate.f ...