UITableView设置Cell左滑多个按钮(编辑,删除,置顶等)
一、iOS7不支持cell多个按钮这个时候可以使用一个三方库JZTableViewRowAction,引用类扩展文件并实现其代理方法
JZTableViewRowAction下载地址:http://download.csdn.net/download/chunjunlu/9506344
- (nullable NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath { __weak typeof(self) weakself = self; void(^deleteBabyAction)(UITableViewRowAction *, NSIndexPath *) = ^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) { [weakself deleteAction:self.babyList[indexPath.row]]; [weakself.babyListTable setEditing:false animated:true]; }; UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"删除" handler:deleteBabyAction]; deleteAction.backgroundColor = [UIColor colorWithHexString:@"ff4545"]; void(^setDefaultBabyAction)(UITableViewRowAction *, NSIndexPath *) = ^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) { [weakself setDefaultBaby:self.babyList[indexPath.row]]; [self.babyListTable setEditing:false animated:true]; }; UITableViewRowAction *setDefaultAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"设为当前" handler:setDefaultBabyAction]; setDefaultAction.backgroundColor = [UIColor colorWithHexString:@"ffa902"]; return @[deleteAction,setDefaultAction]; }
二、iOS8以后可以直接使用系统设置
* tableView:editActionsForRowAtIndexPath: // 设置滑动删除时显示多个按钮
* UITableViewRowAction // 通过此类创建按钮
* 1. 我们在使用一些应用的时候,在滑动一些联系人的某一行的时候,会出现删除、置顶、更多等等的按钮,在iOS8之前,我们都需要自己去实现。But,到了iOS8,系统已经写好了,只需要一个代理方法和一个类就搞定了
* 2. iOS8的协议多了一个方法,返回值是数组的tableView:editActionsForRowAtIndexPath:方法,我们可以在方法内部写好几个按钮,然后放到数组中返回,那些按钮的类就是UITableViewRowAction
* 3. 在UITableViewRowAction类,我们可以设置按钮的样式、显示的文字、背景色、和按钮的事件(事件在Block中实现)
* 4. 在代理方法中,我们可以创建多个按钮放到数组中返回,最先放入数组的按钮显示在最右侧,最后放入的显示在最左侧
* 5. 注意:如果我们自己设定了一个或多个按钮,系统自带的删除按钮就消失了...
/设置滑动时显示多个按钮
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
//添加一个删除按钮
UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:(UITableViewRowActionStyleDestructive) title:@删除 handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
NSLog(@点击了删除);
//1.更新数据
[self.dataArray removeObjectAtIndex:indexPath.row];
//2.更新UI
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationAutomatic)];
}];
//删除按钮颜色
deleteAction.backgroundColor = [UIColor cyanColor];
//添加一个置顶按钮
UITableViewRowAction *topRowAction =[UITableViewRowAction rowActionWithStyle:(UITableViewRowActionStyleDestructive) title:@置顶 handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
NSLog(@点击了置顶);
//1.更新数据
[self.dataArray exchangeObjectAtIndex:indexPath.row withObjectAtIndex:];
//2.更新UI
NSIndexPath *firstIndexPath =[NSIndexPath indexPathForRow: inSection:indexPath.section];
[tableView moveRowAtIndexPath:indexPath toIndexPath:firstIndexPath];
}];
//置顶按钮颜色
topRowAction.backgroundColor = [UIColor magentaColor];
//--------更多
UITableViewRowAction *moreRowAction = [UITableViewRowAction rowActionWithStyle:(UITableViewRowActionStyleNormal) title:@更多 handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
DetailViewController *detailVC = [[DetailViewController alloc]init];
[self.navigationController pushViewController:detailVC animated:YES]; }];
//背景特效
//moreRowAction.backgroundEffect = [UIBlurEffect effectWithStyle:(UIBlurEffectStyleDark)];
//----------收藏
UITableViewRowAction *collectRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@收藏handler:^(UITableViewRowAction *action,NSIndexPath *indexPath) {
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@收藏 message:@收藏成功 delegate:self cancelButtonTitle:@确定 otherButtonTitles:nil, nil]; [alertView show];
[alertView release];
}];
//收藏按钮颜色
collectRowAction.backgroundColor = [UIColor greenColor]; //将设置好的按钮方到数组中返回
return @[deleteAction,topRowAction,moreRowAction,collectRowAction];
// return @[deleteAction,topRowAction,collectRowAction];
}
UITableView设置Cell左滑多个按钮(编辑,删除,置顶等)的更多相关文章
- 第八篇、UITableView常用功能(左滑出现多个按钮,多选删除等)
1.左滑动出现多个按钮 /** * 只要实现了这个方法,左滑出现按钮的功能就有了 (一旦左滑出现了N个按钮,tableView就进入了编辑模式, tableView.editing = YES) */ ...
- iOS开发——UI进阶篇(四)tableView的全局刷新,局部刷新,左滑操作,左滑出现更多按钮,进入编辑模式,批量删除,自定义批量删除
首先创建项目,在storyboard如下布局控件,设置好约束 然后创建cell模型类XMGWineCell数据模型类XMGWine创建UITableView,设置数据源协议,实现数据源方法懒加载数据这 ...
- ios cell左滑删除
iOS项目开发小技能 (三) -UITableView实现Cell左划删除等自定义功能 www.MyException.Cn 网友分享于:2015-06-05 浏览:0次 iOS项目开发小技巧 ...
- iOS cell左滑出现多个功能按钮(IOS8以后支持)
#import "ViewController.h" #import "Swift_OC-Swift.h" @interface ViewController ...
- UITableView设置cell为不可选?
本文选自StackOverflow(简称:SOF)精选问答汇总系列文章之一,本系列文章将为读者分享国外最优质的精彩问与答,供读者学习和了解国外最新技术.本文将为读者讲解UITableView如何设置单 ...
- tableview左滑按钮 tableviewcell自定义左滑按钮
当我们在使用tableview时,往往需要在cell左滑时显示一个或是多个按钮,但系统默认的只可显示一个,如常见的删除按钮,那么当我们的需求要求要有多个按钮时又该怎么办呢,我们往下看. 首先,现看看系 ...
- cell上的按钮点击和左滑冲突
cell上的某个按钮的点击事件,当cell左滑的时候,只要活动的区域也在按钮上,那么按钮的点击事件也会调用. fix: 给按钮添加一个手势(TapGesture)那么当点击的时候就会响应点击手势的方法 ...
- UITableViewCell左滑的时候添加多个按钮的方法(iOS8+)以及UIRefreshControl(iOS6+)的使用。
之前想在cell左滑的时候添加更多的按钮而不是只有‘删除’按钮如下所示,貌似不是一件简单的事.但是现在只要实现几个方法就行了. 代码写的比较垃圾,重在理解这个知识.. . 具体代码: // // T ...
- iOS UITableView左滑操作功能的实现(iOS8-11)
WeTest 导读 本文主要是介绍下iOS 11系统及iOS 11之前的系统在实现左滑操作功能上的区别,及如何自定义左滑的标题颜色.字体大小. 一.左滑操作功能实现 1.如果左滑的时候只有一个操作按钮 ...
随机推荐
- 九度OJ 1145:Candy Sharing Game(分享蜡烛游戏) (模拟)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:248 解决:194 题目描述: A number of students sit in a circle facing their teac ...
- 九度OJ 1038:Sum of Factorials(阶乘的和) (DP、递归)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:1845 解决:780 题目描述: John von Neumann, b. Dec. 28, 1903, d. Feb. 8, 1957, ...
- On Using Very Large Target Vocabulary for Neural Machine Translation Candidate Sampling Sampled Softmax
[softmax分类器的加速器] https://www.tensorflow.org/api_docs/python/tf/nn/sampled_softmax_loss This is a fas ...
- Intellij Idea生成JavaDoc
JavaDoc是一种将注释生成HTML文档的技术,生成的HTML文档类似于Java的API,易读且清晰明了.在简略介绍JavaDoc写法之后,再看一下在Intellij Idea 中如何将代码中的注释 ...
- GIT笔记:将项目发布到码云
GIT笔记:将项目发布到码云 发布步骤 1.码云创建项目 记录下项目的远程地址: https://gitee.com/mrsaber/ms_supplyAndSale.git 2.在本地创建GIT仓库 ...
- Django的模型层(2)---多表操作
多表操作 创建模型 实例:我们来假定下面这些概念,字段和关系 作者模型:一个作者有姓名和年龄. 作者详细模型:把作者的详情放到详情表,包含生日,手机号,家庭住址等信息.作者详情模型和作者模型之间是一对 ...
- openocd+jlink为mini2440调试u-boot
需要安装openocd,如果已经安装了系统默认的openocd(默认是0.5.0,版本太低),需要先卸载掉. 在安装前需要安装必需的一些库文件: -dev libusb-1.0-0 automake ...
- poj 3041 Asteroids(二分图 *【矩阵实现】【最小点覆盖==最大匹配数】)
Asteroids Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16379 Accepted: 8930 Descri ...
- 9--RESTful支持
1.对url进行规范,写RESTful格式的url 非REST的url:http://...../queryItems.action?id=001&type=T01 REST的url风格:ht ...
- PHP ServerPush (推送) 技术的探讨【转】
随着人们对Web即时应用需求的不断上升,Server Push(推送)技术在聊天.消息提醒尤其是社交网络等方面开始兴起,成为实时应用的数据流核心.这篇日志试图探讨的便是各种适合于PHP的Push的实现 ...