ios8 tableView设置滑动删除时 显示多个按钮
|
** * tableView:editActionsForRowAtIndexPath: //设置滑动删除时显示多个按钮 * UITableViewRowAction //通过此类创建按钮 * 1. 我们在使用一些应用的时候,在滑动一些联系人的某一行的时候,会出现删除、置顶、更多等等的按钮,在iOS8之前,我们都需要自己去实现。但是,到了iOS8,系统已经写好了,只需要一个代理方法和一个类就搞定了 * 2. iOS8的协议多了一个方法,返回值是数组的tableView:editActionsForRowAtIndexPath:方法,我们可以在方法内部写好几个按钮,然后放到数组中返回,那些按钮的类就是UITableViewRowAction * 3. 在UITableViewRowAction类,我们可以设置按钮的样式、显示的文字、背景色、和按钮的事件(事件在Block中实现) * 4. 在代理方法中,我们可以创建多个按钮放到数组中返回,最先放入数组的按钮显示在最右侧,最后放入的显示在最左侧 * 5. 注意:如果我们自己设定了一个或多个按钮,系统自带的删除按钮就消失了... |
//
// ViewController.m
// UITableView-Demo
//
// Created by wq-gril on 15/10/20
// Copyright (c) 2015年 . All rights reserved.
//
#import "ViewController.h"
@interfaceViewController () <UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,retain) UITableView *tableView;
@property (nonatomic,retain) NSMutableArray *allDataArray;
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
[selfaddAllViews];
[selfloadAllData];
}
#pragma mark 添加全部页面元素
- (void)addAllViews
{
self.tableView = [[UITableViewalloc] initWithFrame:[UIScreenmainScreen].boundsstyle:UITableViewStylePlain];
_tableView.delegate =self;
_tableView.dataSource =self;
[self.viewaddSubview:_tableView];
#warning iOS8 - 分割线样式
UIBlurEffect *blurEffect = [UIBlurEffecteffectWithStyle:UIBlurEffectStyleDark];
UIVibrancyEffect *vibrancyEffect = [UIVibrancyEffecteffectForBlurEffect:blurEffect];
_tableView.separatorEffect = blurEffect;
//注册
[_tableViewregisterClass:[UITableViewCellclass]forCellReuseIdentifier:@"cellIdentifier"];
//右上角编辑按钮
self.navigationItem.rightBarButtonItem =self.editButtonItem;
}
#pragma mark 加载所有数据
- (void)loadAllData
{
self.allDataArray =[[NSMutableArrayalloc]initWithObjects:@"王菲",@"周迅",@"李冰冰",@"白冰",@"紫薇",@"马苏",@"刘诗诗",@"赵薇",@"angelbaby",@"熊黛林",nil];
}
#pragma mark 编辑按钮
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
[supersetEditing:editing animated:animated];
[_tableViewsetEditing:!_tableView.isEditinganimated:YES];
}
#pragma mark - UITableViewDataSource Methods
#pragma mark 设置有多少分组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return1;
}
#pragma mark 设置每个分组有多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return_allDataArray.count;
}
#pragma mark 设置某行上显示的内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:@"cellIdentifier"forIndexPath:indexPath];
cell.textLabel.text =_allDataArray[indexPath.row];
// cell.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 256.0 green:arc4random() % 256 / 256.0 blue:arc4random() % 256 / 256.0 alpha:1.0f];
return cell;
}
#pragma mark 设置可以进行编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath*)indexPath
{
returnYES;
}
#pragma mark 设置编辑的样式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
returnUITableViewCellEditingStyleDelete;
}
#pragma mark 设置处理编辑情况
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle ==UITableViewCellEditingStyleDelete) {
// 1.更新数据
[_allDataArrayremoveObjectAtIndex:indexPath.row];
// 2.更新UI
[tableViewdeleteRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationAutomatic];
}
}
#pragma mark 设置可以移动
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath*)indexPath
{
returnYES;
}
#pragma mark 处理移动的情况
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath*)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
// 1. 更新数据
NSString *title =_allDataArray[sourceIndexPath.row];
[_allDataArrayremoveObject:title];
[_allDataArrayinsertObject:title atIndex:destinationIndexPath.row];
// 2. 更新UI
[tableView moveRowAtIndexPath:sourceIndexPathtoIndexPath:destinationIndexPath];
}
/**
* tableView:editActionsForRowAtIndexPath: //设置滑动删除时显示多个按钮
* UITableViewRowAction //通过此类创建按钮
* 1. 我们在使用一些应用的时候,在滑动一些联系人的某一行的时候,会出现删除、置顶、更多等等的按钮,在iOS8之前,我们都需要自己去实现。But,到了iOS8,系统已经写好了,只需要一个代理方法和一个类就搞定了
* 2. iOS8的<UITableViewDelegate>协议多了一个方法,返回值是数组的tableView:editActionsForRowAtIndexPath:方法,我们可以在方法内部写好几个按钮,然后放到数组中返回,那些按钮的类就是UITableViewRowAction
* 3. 在UITableViewRowAction类,我们可以设置按钮的样式、显示的文字、背景色、和按钮的事件(事件在Block中实现)
* 4. 在代理方法中,我们可以创建多个按钮放到数组中返回,最先放入数组的按钮显示在最右侧,最后放入的显示在最左侧
* 5. 注意:如果我们自己设定了一个或多个按钮,系统自带的删除按钮就消失了...
*/
#warning iOS8 -
#pragma mark 在滑动手势删除某一行的时候,显示出更多的按钮
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 添加一个删除按钮
UITableViewRowAction *deleteRowAction = [UITableViewRowActionrowActionWithStyle:UITableViewRowActionStyleDestructivetitle:@"删除"handler:^(UITableViewRowAction *action,NSIndexPath *indexPath) {
NSLog(@"点击了删除");
// 1. 更新数据
[_allDataArrayremoveObjectAtIndex:indexPath.row];
// 2. 更新UI
[tableViewdeleteRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationAutomatic];
}];
// // 删除一个置顶按钮
// UITableViewRowAction *topRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"置顶" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
// NSLog(@"点击了置顶");
//
// // 1. 更新数据
// [_allDataArray exchangeObjectAtIndex:indexPath.row withObjectAtIndex:0];
//
// // 2. 更新UI
// NSIndexPath *firstIndexPath = [NSIndexPath indexPathForRow:0 inSection:indexPath.section];
// [tableView moveRowAtIndexPath:indexPath toIndexPath:firstIndexPath];
// }];
// topRowAction.backgroundColor = [UIColor blueColor];
//添加一个更多按钮
UITableViewRowAction *moreRowAction = [UITableViewRowActionrowActionWithStyle:UITableViewRowActionStyleNormaltitle:@"更多"handler:^(UITableViewRowAction *action,NSIndexPath *indexPath) {
NSLog(@"点击了更多");
[tableViewreloadRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationMiddle];
}];
moreRowAction.backgroundEffect = [UIBlurEffecteffectWithStyle:UIBlurEffectStyleDark];
//将设置好的按钮放到数组中返回
// return @[deleteRowAction, topRowAction, moreRowAction];
return@[deleteRowAction,moreRowAction];
}
ios8 tableView设置滑动删除时 显示多个按钮的更多相关文章
- extjs combobox 设置下拉时显示滚动条 设置显示条数
extjs在点击下拉时如果没有限制它的高度,那么它的默认最大高度是300,显示的时候就会显示300的高度,知道选项内容超过这个高度时才会自动显示滚动条,往往在有些时候我们希望让combobox显示一个 ...
- 设置滑动TabBar的显示和隐藏
代码如下: //设置滑动的判定范围 - (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint ...
- 如何在PL/SQL Developer 中设置 在select时 显示所有的数据
在执行select 时, 总是不显示所有的记录, 要点一下, 下面那个按钮才会显示所有的数据. 解决方法: Tools>Preferences>Window Types>SQ ...
- DotNetBar 控件设置空内容时显示内容
可以通过修改所有关于Watermark时的设置.
- Ubuntu 16.04设置开机关机时显示命令详细信息不显示进度条Logo
1.编辑grub文件 sudo gedit /etc/default/grub 把 GRUB_CMDLINE_LINUX_DEFAULT="quiet splash" 改成 GRU ...
- Vue:列表展开和收起(超过一定行数时显示‘查看更多’按钮)
前言:前端小白记录的一些小功能~ 公司开发中的小程序中有做任务签到的功能,这就涉及到了任务列表以及对任务列表的展开和收起功能,好了可以开始了,说多了就烦了 1.首先是css样式,因为设计稿上是超过两行 ...
- tableviewcell滑动显示多个按钮UITableViewRowAction(转载)
demo截图 ios8 新的属性 typedef NS_ENUM(NSInteger, UITableViewRowActionStyle) { UITableViewRowActionStyleDe ...
- 仿知乎安卓client滑动删除撤销ListView
标签(空格分隔): Android 新版的知乎安卓client有一个有趣的功能,就是在一个item里.向右滑动时整个item会越来越透明,滑动到一半时,整个item就不见了.放开手指就是删除.删除后还 ...
- 设置TabBarItem选中时的图片及文字颜色
TabBarItem选中时,默认文字和图片都变为蓝色.使用以下代码可以进行修改. MainViewController *mainVC = [[MainViewController alloc] in ...
随机推荐
- java.lang.UnsupportedClassVersionError: org/xwiki/xxx : Unsupported major.minor version 51.0
此类问题主要是因为Unsupported major.minor version 51.0. 原因是JDK版本不一致导致的问题.在web应用中碰到此问题. 应用中规定使用JDK7.0,本地JDK为6. ...
- emc 郵件設置
1. 進入Data Domain管理界面后,在Administration--->Settings界面.點擊More mail Server--->Set Mail Server---&g ...
- Oracle【IT实验室】数据库备份与恢复之四:RMAN(备份与恢复管理器)
RMAN是ORACLE提供的一个备份与恢复的工具,可以用来备份和还原数据库文件. 归档日志和控制文件.它也可以用来执行完全或不完全的数据库恢复. RMAN可以由命令行接口或者 OEM的 Backup ...
- 利用IdentityServer3在ASP.NET 5和Angular中实现OAuth2 Implicit Flow
(此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:之前介绍过的IdentityServer3虽然是基于Katana开发的,不过同样可以托 ...
- 解决linux下unzip中文有乱码的问题
xxx.zip 中有中文的文件,在linux下unzip就会有乱码. 解决办法:安装7zip 去http://sourceforge.net/projects/p7zip/files/latest/d ...
- dwz中权限的控制
很多人不明白用dwz要如何在没有登录的时候跳转到登录页面,没有权限的时候弹出提示. 其实,作者在设计的时候,已经完全考虑到了这些需求. 不管是navTab还是dialog,dwz的页面加载最终都是通过 ...
- 在Visualforce page中用自带的控件实现Ajax回调后台方法(并且可以用js去动态给parameters赋值)
这里用的组合是:apex:commandLink + apex:actionFunction + apex:outputPanel 这里的 apex:commandLink 和 apex:actio ...
- ubuntu12.04 安装eclipse
1:去官网下载最新版的eclipse for linux; 2:cd /usr/local 用命令 sudo mkdir eclipse 建立一个Eclipse的目录 3:将下载的文件copy到ec ...
- canvas加载进度条
<!DOCTYPE html> <html><head><meta http-equiv="Content-Type" content=& ...
- Arduino101学习笔记(三)—— 101简介
一.板子图示--摘自中文社区 二.技术规格 主控器 Intel Curie 工作电压 3.3V (I/O兼容5V) 输入电压 (推荐) 7-12V 输入电压 (极限) 6-20V 数字 I/O 14 ...