自定义uitableviewcell通过加上滑动手势进行删除对应的行。PS:用代理来实现
#import <UIKit/UIKit.h>
@class ZSDCustomCell;
//协议
@protocol ZSDCustomCellDelegate <NSObject>
//判断选择某行以及某行中的按钮
-(void)deleteDidSelectCell:(ZSDCustomCell *)customCell andClickButton:(int)selectButtonIndex;
@end
@interface ZSDCustomCell : UITableViewCell<UIGestureRecognizerDelegate>
//显示内容的label标签
@property (weak, nonatomic) IBOutlet UILabel *contentLabel;
//右边红色的view
@property (weak, nonatomic) IBOutlet UIView *rightView;
//删除按钮
@property (weak, nonatomic) IBOutlet UIButton *deleteBtn;
//代理
@property(weak,nonatomic)id<ZSDCustomCellDelegate>delegate;
@end
#import "ZSDCustomCell.h"
@implementation ZSDCustomCell
-(void)awakeFromNib
{
//左滑动
UISwipeGestureRecognizer *swipeLeft=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipe:)];
//右滑动
UISwipeGestureRecognizer *swipeRight=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipe:)];
//单击手势
// UITapGestureRecognizer *singleTap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleSingleTap:)];
[swipeLeft setNumberOfTouchesRequired:1];
[swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
[swipeRight setNumberOfTouchesRequired:1];
[swipeRight setDirection:UISwipeGestureRecognizerDirectionRight];
//[self addGestureRecognizer:singleTap];
[self addGestureRecognizer:swipeLeft];
[self addGestureRecognizer:swipeRight];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
-(void)handleSwipe:(UISwipeGestureRecognizer *)gesture
{
//如果是向左滑,显示右边的view
if (gesture.direction==UISwipeGestureRecognizerDirectionLeft)
{
[UIView animateWithDuration:1.0 animations:^{
[self.deleteBtn addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
self.rightView.hidden=NO;
}];
}
//如果是向右滑,隐藏右边的view
else if (gesture.direction==UISwipeGestureRecognizerDirectionRight)
{
[UIView animateWithDuration:1.0 animations:^{
self.rightView.hidden=YES;
}];
}
}
//按钮事件
-(void)buttonAction:(UIButton *)sender
{
if (_delegate&&[_delegate respondsToSelector:@selector(deleteDidSelectCell:andClickButton:)])
{
[_delegate deleteDidSelectCell:self andClickButton:(int)sender.tag];
}
}
/*
- (void)handleSingleTap:(UITapGestureRecognizer *)gestureRecognizer
{
[UIView animateWithDuration:0.5 animations:^
{
self.rightView.hidden=NO;
}];
}
*/
@end
#import <UIKit/UIKit.h>
@interface ZSDViewController : UIViewController
@end
#import "ZSDViewController.h"
#import "ZSDCustomCell.h"
@interface ZSDViewController ()<UITableViewDataSource,UITableViewDelegate,ZSDCustomCellDelegate>
@property(nonatomic,strong)NSMutableArray *dataArray;
@property (weak, nonatomic) IBOutlet UITableView *myTableView;
@end
@implementation ZSDViewController
#pragma mark - life circle
- (void)viewDidLoad
{
[super viewDidLoad];
//已经在storyboard上进行设置代理,这里不需要在写
//_myTableView.dataSource=self;
//_myTableView.delegate=self;
self.myTableView.separatorStyle=UITableViewCellSeparatorStyleSingleLine;
for (int i=0; i<10; i++)
{
[[self dataArray] addObject:[NSString stringWithFormat:@"%d",i]];
}
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - private
-(NSMutableArray *)dataArray
{
if (_dataArray==nil)
{
_dataArray=[NSMutableArray array];
}
return _dataArray;
}
#pragma mark - UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _dataArray.count;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
ZSDCustomCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell"];
cell.delegate=self;
cell.rightView.hidden=YES;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.deleteBtn.tag=indexPath.row;
cell.contentLabel.text=_dataArray[indexPath.row];
cell.tag=indexPath.row;
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 80;
}
#pragma mark - ZSDCustomCellDelegate
-(void)deleteDidSelectCell:(ZSDCustomCell *)customCell andClickButton:(int)selectButtonIndex
{
int result=selectButtonIndex;
if (customCell.tag==result)
{
NSIndexPath *indexPath=[NSIndexPath indexPathForRow:result inSection:0];
[self.dataArray removeObjectAtIndex:result];
[self.myTableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
[self.myTableView reloadData];
}
}
@end
自定义uitableviewcell通过加上滑动手势进行删除对应的行。PS:用代理来实现的更多相关文章
- 自定义UITableViewCell实现左滑动多菜单功能LeftSwipe
今天愚人节,小伙们,愚人节快乐! 实现一个小功能,滑动菜单,显示隐藏的功能菜单, 先上图: 这里尝试用了下使用三个方式来实现了这个功能: 1.使用自定义UI ...
- 自定义UITableViewCell 的delete按钮
自定义UITableViewCell上的delete按钮 滑动列表行(UITableViewCell)出现删除按钮时,默认是英文“delete”,这份代码片段能够将“delete”变成中文”删除“,甚 ...
- 如何得到自定义UITableViewCell中的按钮所在的cell的indexPath.row
在自定义UITableViewCell中创建了一个按钮. 想在点击该按钮时知道该按钮所在的cell在TableView中的行数.就是cell的 indexPath.row两种方法都很好.-(IBAct ...
- ios滑动手势全屏(这段代码实现了下一级控制器滑到上一级控制器)
在自定义导航控制器里面加以下代码就增加全屏滑动手势 >推向前一个控制器 // HBNavigationController.m // #import "HBNavigationCon ...
- 新手教程之使用Xib自定义UITableViewCell
新手教程之使用Xib自定义UITableViewCell 前言 首先:什么是UITableView?看图 其次:什么是cell? 然后:为什么要自定cell,UITableView不是自带的有cell ...
- 自定义UITableViewCell的方法
1.纯XIB/storyboard自定义.对应一个Controller的storyboard上拖拽出一个自定义Cell,并加上ReuseIdentifitor 2.纯代码自定义,通过在contentV ...
- 解决嵌套在ScrollView中的TableView滑动手势冲突问题
最近在迭代开发公司项目的时候遇到了一个问题,在可以左右切换标签视图的ScrollView中嵌套了两个TableView用于展示视图,感觉一切so easy的情况下,问题出现了,因为左右两个视图既可以实 ...
- 【转】自定义UITableViewCell(registerNib: 与 registerClass: 的区别)
自定义UITableViewCell大致有两类方法: 使用nib 1.xib中指定cell的Class为自定义cell类型(注意不是设置File's Owner的class) 2.调用 tableVi ...
- ios开发UI篇—使用纯代码自定义UItableviewcell实现一个简单的微博界面布局
本文转自 :http://www.cnblogs.com/wendingding/p/3761730.html ios开发UI篇—使用纯代码自定义UItableviewcell实现一个简单的微博界面布 ...
随机推荐
- 汇编语言程序入门实验二:在dos下建立子目录操作
汇编语言程序入门实验二:在dos下建立子目录操作 1,背景 在读此文,并读懂前,建议读者先阅读这两篇博客 1,在dos环境下汇编语言程序设计入门(输出hello world)和masm32的下载.安装 ...
- UI进阶 地图
一.地图的简介 在移动互联网时代,移动app能解决用户的很多生活琐事,比如 导航:去任意陌生的地方 周边:找餐馆.找酒店.找银行.找电影院 手机软件:微信摇一摇.QQ附近的人.微博. ...
- WCF WEB API配置
Web.config配置 <system.serviceModel> <services> <service name="WCFServiceWebRole2. ...
- C:结构体
结构体 构造类型:就是有基本的类型组成的 1.结构体 结构体是一种自定义的数据类型 和 int float 是一样的都可以定义变量 数组 只能存放一种类型的容器 结构体 可以存放多种数据类型 ...
- (C++)STL排序函数sort和qsort的用法与区别
主要内容: 1.qsort的用法 2.sort的用法 3.qsort和sort的区别 qsort的用法: 原 型: void qsort(void *base, int nelem, int widt ...
- 【转】【Android测试技巧】01. root后adb shell默认不是root用户时,如何将文件放入手机系统中
http://blog.csdn.net/wirelessqa/article/details/8624208 有些机器root后通过adb shell 后,默认不是root用户,需要输入 su才能切 ...
- X431 元征诊断枪
X-431 Diagun是专门为汽车维修技师设计的诊断设备. 小巧的主机.强大的诊断功能.方便快捷的网上升级.一体化多功能接头,都是维修技师的首选.X-431 Diagun 是汽车维修技师的标准装备. ...
- PCA和白化练习之处理图像
第一步:下载pca_exercise.zip,里面包含有图像数据144*10000,每一列代表一幅12*12的图像块,首先随见展示200幅: 第二步:0均值处理,确保数据均值为0或者接近0 第三步:执 ...
- Immutable.js尝试(node.js勿入)
最近做一些复杂html常常需要在页面做一些数据处理,常常在想如果 js有list 这种数据结构多少,今天逛github时 发现有Immutable.js 这个项目https://github.com/ ...
- 实战项目:通过当当API将订单抓取到SAP(二)
上一篇博客,我们引用了log4net 这个.这里简单介绍下,为什么引用这个. log4net是记录程序日志信息的,是一个功能著名的开源日志记录组件.利用log4net可以方便地将日志信息记录到文件.控 ...