#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:用代理来实现的更多相关文章

  1. 自定义UITableViewCell实现左滑动多菜单功能LeftSwipe

    今天愚人节,小伙们,愚人节快乐! 实现一个小功能,滑动菜单,显示隐藏的功能菜单, 先上图:                       这里尝试用了下使用三个方式来实现了这个功能: 1.使用自定义UI ...

  2. 自定义UITableViewCell 的delete按钮

    自定义UITableViewCell上的delete按钮 滑动列表行(UITableViewCell)出现删除按钮时,默认是英文“delete”,这份代码片段能够将“delete”变成中文”删除“,甚 ...

  3. 如何得到自定义UITableViewCell中的按钮所在的cell的indexPath.row

    在自定义UITableViewCell中创建了一个按钮. 想在点击该按钮时知道该按钮所在的cell在TableView中的行数.就是cell的 indexPath.row两种方法都很好.-(IBAct ...

  4. ios滑动手势全屏(这段代码实现了下一级控制器滑到上一级控制器)

    在自定义导航控制器里面加以下代码就增加全屏滑动手势 >推向前一个控制器 //  HBNavigationController.m // #import "HBNavigationCon ...

  5. 新手教程之使用Xib自定义UITableViewCell

    新手教程之使用Xib自定义UITableViewCell 前言 首先:什么是UITableView?看图 其次:什么是cell? 然后:为什么要自定cell,UITableView不是自带的有cell ...

  6. 自定义UITableViewCell的方法

    1.纯XIB/storyboard自定义.对应一个Controller的storyboard上拖拽出一个自定义Cell,并加上ReuseIdentifitor 2.纯代码自定义,通过在contentV ...

  7. 解决嵌套在ScrollView中的TableView滑动手势冲突问题

    最近在迭代开发公司项目的时候遇到了一个问题,在可以左右切换标签视图的ScrollView中嵌套了两个TableView用于展示视图,感觉一切so easy的情况下,问题出现了,因为左右两个视图既可以实 ...

  8. 【转】自定义UITableViewCell(registerNib: 与 registerClass: 的区别)

    自定义UITableViewCell大致有两类方法: 使用nib 1.xib中指定cell的Class为自定义cell类型(注意不是设置File's Owner的class) 2.调用 tableVi ...

  9. ios开发UI篇—使用纯代码自定义UItableviewcell实现一个简单的微博界面布局

    本文转自 :http://www.cnblogs.com/wendingding/p/3761730.html ios开发UI篇—使用纯代码自定义UItableviewcell实现一个简单的微博界面布 ...

随机推荐

  1. vim之pydiction插件

    [vim之pydiction插件] It consists of three main files: python_pydiction.vim -- Vim plugin. complete-dict ...

  2. USB设备不能用。提示Windows 无法启动这个硬件设备。 (代码 19)

    USB,由于其配置信息(注册表中的)不完整或已损坏, Windows 无法启动这个硬件设备. (代码 19) 原因:提示Windows 无法启动这个硬件设备. (代码 19) 处理解决方法: 1) r ...

  3. CISCO3560 VLAN配置实例

    1.注意事项 1.1.交换机启动需要大约4-5分钟: 1.2.网线插入交换机接口从黄变为绿需要大约1-2分钟,即进入正常工作模式: 1.3.建议使用XP系统进行操作,2003默认没有安装超级终端,需要 ...

  4. lambda表达式和ef的语句转化

    这两者转化可以用linqpad进行转化, 首先推荐一个网站可以了解一下orderby的排序方式 http://www.csharpwin.com/csharpspace/614.shtml 然后下面有 ...

  5. WIN7建立wifi热点及无法启动承载网络的解决办法

    1,根据网络共享的方法,最简单莫过于利用Win7的虚拟网卡来做热点,而不用借助其他软件.         首先,用管理员身份打开CMD命令提示符,输入 netsh wlan set hostednet ...

  6. 利用HTML5开发Android(2)---Android中构建HTML5应用

    使用WebView控件 与其他控件的使用方法相同 在layout中使用一个<WebView>标签 WebView不包括导航栏,地址栏等完整浏览器功能,只用于显示一个网页 在WebView中 ...

  7. CountDownLatch和CyclicBarrier的区别

    [CountDownLatch.CyclicBarrier和Semaphore]http://www.cnblogs.com/dolphin0520/p/3920397.html   [CountDo ...

  8. 利用Notepad++插件(JSToolNpp):让JS代码格式化对齐

    http://www.ycyjkj.com/post-663.html 程序员或者正在学习的同学一般都会遇到一个问题,看别人的源码,只是一行,没有分开,让人看的很别扭,也可能是作者故意这样做的,也可以 ...

  9. PHP抓取豆瓣读书爬虫代码

    <?php//演示地址 http://asizu.sinaapp.com/reptile_douban.php//数据量不是特别大,没有写抓完数据便停止. 喜欢的朋友拿去自己改改就好了 head ...

  10. 批量更改数据库COLLATION

    企业内部有很多系统是繁体的,由于各方面的原因,公司目前正在实行简体化,但各系统中又有数据间的交换,所以系统只能一个一个的更改,以防同时出现过多的问题.由于原先数据库只能存储繁体,而原先已存在的数据则可 ...