之前想在cell左滑的时候添加更多的按钮而不是只有‘删除’按钮如下所示,貌似不是一件简单的事。但是现在只要实现几个方法就行了。

代码写的比较垃圾,重在理解这个知识。。

具体代码:

//

//  TableViewController.m

//  ios8_tableview(左滑添加按钮)

//

//  Created by mudy on 15/8/21.

//  Copyright (c) 2015年 mudy. All rights reserved.

//

#import "TableViewController.h"

@interface TableViewController ()

@property (nonatomic,strong)NSMutableArray *dataSource;

@end

@implementation TableViewController

- (void)viewDidLoad {

[super viewDidLoad];

self.dataSource = [NSMutableArray array];

self.dataSource = [@[@"mudy1",@"mudy2",@"mudy3",@"mudy4",@"mudy5",@"mudy6"]mutableCopy];

// Uncomment the following line to preserve selection between presentations.

// self.clearsSelectionOnViewWillAppear = NO;

// Uncomment the following line to display an Edit button in the navigation bar for this view controller.

self.navigationItem.rightBarButtonItem = self.editButtonItem;

self.refreshControl = [UIRefreshControl new];

self.refreshControl.tintColor = [UIColor redColor];

[self.refreshControl addTarget:self action:@selector(refreshControl:) forControlEvents:UIControlEventValueChanged];

UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]

initWithTarget:self action:@selector(longPressGestureRecognized:)];

[self.tableView addGestureRecognizer:longPress];

}

- (IBAction)longPressGestureRecognized:(UILongPressGestureRecognizer *)sender {

UILongPressGestureRecognizer *longPress = (UILongPressGestureRecognizer *)sender;

UIGestureRecognizerState state = longPress.state;

CGPoint location = [longPress locationInView:self.tableView];

NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];

static UIView       *snapshot = nil;        ///< A snapshot of the row user is moving.

static NSIndexPath  *sourceIndexPath = nil; ///< Initial index path, where gesture begins.

switch (state) {

case UIGestureRecognizerStateBegan: {

if (indexPath) {

sourceIndexPath = indexPath;

UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];

// Take a snapshot of the selected row using helper method.

snapshot = [self customSnapshotFromView:cell];

// Add the snapshot as subview, centered at cell's center...

__block CGPoint center = cell.center;

snapshot.center = center;

snapshot.alpha = 0.0;

[self.tableView addSubview:snapshot];

[UIView animateWithDuration:0.25 animations:^{

// Offset for gesture location.

center.y = location.y;

snapshot.center = center;

snapshot.transform = CGAffineTransformMakeScale(1.05, 1.05);

snapshot.alpha = 0.98;

// Black out.

cell.backgroundColor = [UIColor blackColor];

} completion:nil];

}

break;

}

case UIGestureRecognizerStateChanged: {

CGPoint center = snapshot.center;

center.y = location.y;

snapshot.center = center;

// Is destination valid and is it different from source?

if (indexPath && ![indexPath isEqual:sourceIndexPath]) {

// ... update data source.

[self.dataSource exchangeObjectAtIndex:indexPath.row withObjectAtIndex:sourceIndexPath.row];

// ... move the rows.

[self.tableView moveRowAtIndexPath:sourceIndexPath toIndexPath:indexPath];

// ... and update source so it is in sync with UI changes.

sourceIndexPath = indexPath;

}

break;

}

default: {

// Clean up.

UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:sourceIndexPath];

[UIView animateWithDuration:0.25 animations:^{

snapshot.center = cell.center;

snapshot.transform = CGAffineTransformIdentity;

snapshot.alpha = 0.0;

// Undo the black-out effect we did.

cell.backgroundColor = [UIColor whiteColor];

} completion:^(BOOL finished) {

[snapshot removeFromSuperview];

snapshot = nil;

}];

sourceIndexPath = nil;

break;

}

}

}

- (UIView *)customSnapshotFromView:(UIView *)inputView {

UIView *snapshot = [inputView snapshotViewAfterScreenUpdates:YES];

snapshot.layer.masksToBounds = NO;

snapshot.layer.cornerRadius = 0.0;

snapshot.layer.shadowOffset = CGSizeMake(-1.0, 0.0);

snapshot.layer.shadowRadius = 1.0;

snapshot.layer.shadowOpacity = 0.4;

return snapshot;

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

-(void)refreshControl:(id)sender{

[NSThread sleepForTimeInterval:5.0];//刷新5秒钟

self.refreshControl.attributedTitle = [[NSAttributedString alloc]initWithString:@"刷新ing"];

[self.tableView reloadData];

[self.refreshControl endRefreshing];

NSLog(@"really?");

}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

// Return the number of sections.

return 1;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

// Return the number of rows in the section.

return self.dataSource.count;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];

cell.textLabel.text = [NSString stringWithFormat:@"%@",self.dataSource[indexPath.row]];

// Configure the cell...

return cell;

}

// Override to support conditional editing of the table view.

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {

// Return NO if you do not want the specified item to be editable.

return YES;

}

// Override to support editing the table view.

//这个方法并没有走,但是也不能删掉这个方法

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

if (editingStyle == UITableViewCellEditingStyleDelete) {

} else if (editingStyle == UITableViewCellEditingStyleInsert) {

// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view

}

}

-(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];

UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"删除" message:@"删除成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];

[alertView show];

}];

//设置收藏按钮

UITableViewRowAction *collection = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"收藏" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {

UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"收藏" message:@"收藏成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];

[alertView show];

[self.tableView reloadData];

}];

//设置置顶按钮

UITableViewRowAction *topRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"置顶" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {

topRowAction.backgroundColor = [UIColor yellowColor];

[self.dataSource exchangeObjectAtIndex:indexPath.row withObjectAtIndex:0];

NSIndexPath *first = [NSIndexPath indexPathForRow:0 inSection:indexPath.section];

//        NSIndexPath *f =[NSIndexPath inde]

[tableView moveRowAtIndexPath:indexPath toIndexPath:first];

}];

deleteRowAction.backgroundEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];

topRowAction.backgroundColor = [UIColor blueColor];

deleteRowAction.backgroundColor = [UIColor grayColor];

collection.backgroundColor = [UIColor brownColor];

return @[deleteRowAction,collection,topRowAction];

}

// Override to support rearranging the table view.

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {

[tableView moveRowAtIndexPath:fromIndexPath toIndexPath:toIndexPath];

}

// Override to support conditional rearranging of the table view.

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {

// Return NO if you do not want the item to be re-orderable.

return YES;

}

/*

#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

// Get the new view controller using [segue destinationViewController].

// Pass the selected object to the new view controller.

}

*/

@end

UITableViewCell左滑的时候添加多个按钮的方法(iOS8+)以及UIRefreshControl(iOS6+)的使用。的更多相关文章

  1. UITableViewCell 左滑删除

    - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { return Y ...

  2. 使用zepto实现QQ消息左滑删除效果

    有这样一个需求: 1. 有一个列表,将每一个列表项左滑动出现删除按钮: 2. 右滑动隐藏删除按钮: 3. 点击这个删除按钮删除该列表项. 完成以后的效果: 这是微信网页端的页面,使用的是 zepto ...

  3. 三种方法为QLineEdit添加清除内容按钮

    很多时候我们会发现输入的一长串内容不得不全部删除重新输入,这时比起一直按着退格键不放一个清除内容按钮更受欢迎. 今天我将介绍三种为QLineEdit添加清除内容按钮的方法,其中两种方法有较强的功能针对 ...

  4. UITableView 编辑模式(增加-删除-移动---自定义左滑 title)

    - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typica ...

  5. UITableView 编辑模式(增加-删除-移动---自定义左滑 title) xib cell

    参考:  http://www.open-open.com/lib/view/open1430008922468.html - (void)viewDidLoad { [super viewDidLo ...

  6. [Xcode 实际操作]五、使用表格-(9)删除UITableView单元格(手势左滑调出删除按钮)

    目录:[Swift]Xcode实际操作 本文将演示如何删除某一行单元格.手势左滑调出删除按钮. 在项目导航区,打开视图控制器的代码文件[ViewController.swift] import UIK ...

  7. ionic ios 左滑 白屏

    之前发现ionic在发布ios之后,左滑屏幕的时候会出现界面变白,但是画面原有的位置点击还是有效的,但是点击之后界面是不正确的,返回到上上一步 然后查找资料发现是ios系统内置的左滑动作造成了影响,修 ...

  8. iOS开发——UI进阶篇(四)tableView的全局刷新,局部刷新,左滑操作,左滑出现更多按钮,进入编辑模式,批量删除,自定义批量删除

    首先创建项目,在storyboard如下布局控件,设置好约束 然后创建cell模型类XMGWineCell数据模型类XMGWine创建UITableView,设置数据源协议,实现数据源方法懒加载数据这 ...

  9. js高仿QQ消息列表左滑功能

    该组件,主要功能类似于QQ消息列表左滑出现删除.标为已读等按钮的功能:现在的版本用的是纯javaScript编写:后续会跟进 angularJs 开发的类似组件以及jquery的; 下面,就让我们来认 ...

随机推荐

  1. [Xcode使用 - 1] 自定义代码块引用

    Xcode本身带有编码常用的代码块可供使用,如下图   例如 “if” 这个代码块的定义     调用方法 1.可以直接拖放带编辑区   2.使用快捷键,键入 “if”, Xcode就会出现自动完成的 ...

  2. RC522 射频读卡器模块(MINI型)

    一.硬件: 二.[主芯片介绍] MF RC522是应用于13.56MHz非接触式通信中高集成度的读写卡芯片,是NXP公司针对"三表"应用推出的一款低电压.低成本.体积小的非接触式读 ...

  3. 关于local storage 和 session storage以及cookie 区别简析

    session storage 和local storage 都是存储在客户端的浏览器内: 一:关于COOKIE 的缺陷 * Cookie的问题 * 数据存储都是以明文(未加密)方式进行存储 * 安全 ...

  4. JS实现项目查找功能

    又是好久没有更新文章了,技术差,人又懒是重罪啊!! 在工作中每天都要查找目前正在接手的项目,而如果项目一多起来怎么办呢? 最近主管突然说要找一下以前的项目改一点BUG,然后我就找了半天才找到对应的文件 ...

  5. web工作方式,浏览网页,打开浏览器,输入网址按下回车键,然后会显示出内容,这个过程是怎样的呢?

    以下内容摘自<Go Web编程>,介绍的通俗易懂. 我们平时浏览网页的时候,会打开浏览器,输入网址后按下回车键,然后就会显示出你想要浏览的内容.在这个看似简单的用户行为背后,到底隐藏了些什 ...

  6. BZOJ 1151 傲娇的人 排序

    傲娇的人 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/problem/show/1151 Descrip ...

  7. android自动打包方法(ant+proguard+签名)

    前段时间做了一个android的网游项目,现在优化减少体积和防止别人反编译,需要把编译后.class进行混淆,开始在网上看了一些关于 ProGuard的介绍,基本上都是使用ADT自带的打包方式,那个打 ...

  8. MSSQL计算日期方法大全

    通常,你需要获得当前日期和计算一些其他的日期,例如,你的程序可能需要判断一个月的第一天或者最后一天.你们大部分人大概都知道怎样把日期进行分割(年.月.日等),然后仅仅用分割出来的年.月.日等放在几个函 ...

  9. ie7兼容性 绝对定位元素之间的遮盖问题

    今天在调试ie7兼容性问题的时候,遇到了一个棘手的问题,解决了一天无果,晚上看到一位大神写的心得,最终解决. 网址:http://www.jb51.net/css/148606.html#commen ...

  10. oracle 基础SQL语句 多表查询 子查询 分页查询 合并查询 分组查询 group by having order by

    select语句学习 . 创建表 create table user(user varchar2(20), id int); . 查看执行某条命令花费的时间 set timing on: . 查看表的 ...