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 1075:斐波那契数列 (数字特性)
时间限制:5 秒 内存限制:32 兆 特殊判题:否 提交:3121 解决:1806 题目描述: 编写一个求斐波那契数列的递归函数,输入n值,使用该递归函数,输出如样例输出的斐波那契数列. 输入: 一个 ...
- toitorsegit and toitorstsvn文件夹icon冲突不显示
Go to HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Explorer Add a new string value & ...
- mongo 数据库提前关闭 避免读写任务没有结束,异步任务没有完成,同步指令提前关闭数据库:'MongoError: server instance pool was destroyed'
mongo 数据库提前关闭 // mongodb - npm https://www.npmjs.com/package/mongodb const mongoCfg = { uri: 'mongod ...
- mysql系列之9.mysql日志&存储引擎
mysqlbinlog 是什么? 数据目录下的如下文件: mysql-bin.xxxxxx 作用? 记录数据库内部增删改查对mysql数据库有更新的内容的记录 三种模式? statement leve ...
- [证书服务器 第二篇] 基于OpenSSL 在 CentOS6 系统上 搭建自签证书服务,并应用于Web容器
第一部分:概述 .. 第二部分:环境准备 1 操作系统 CentOS 6.x 2 安装openssl yum install -y openssl 3 安装jdk 从官网下载JDK http://ww ...
- ME51N, ME52N 创建采购申请的一个BADI
ME51N ME52N创建修改采购申请时的一个BADI (2013-07-11 16:50:58) 转载▼ 标签: 采购申请 me51n me52 badi me_process_req_cust 分 ...
- ZooKeeper原理及使用(转)
原文地址 ZooKeeper是Hadoop Ecosystem中非常重要的组件,它的主要功能是为分布式系统提供一致性协调(Coordination)服务,与之对应的Google的类似服务叫Chubby ...
- java和js互调 webview
public class JavaAndJSActivity extends Activity implements View.OnClickListener { private EditText e ...
- 《CSS权威指南(第三版)》---第一章 CSS和文档
主要学习的知识是怎么把CSS和HTML文档关联: 1.这是默认的样式表 <link rel="stylesheet" href="" type=" ...
- Module.exports和exports的区别
原文链接: https://www.ycjcl.cc/2017/02/10/module-exportshe-exportsde-qu-bie/ 学习Seajs时,看到了exports.doSomet ...