自定义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

时间: 2024-08-24 21:04:57

自定义uitableviewcell通过加上滑动手势进行删除对应的行。PS:用代理来实现的相关文章

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

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

自定义返回按钮后的滑动手势

自定义返回按钮是开发工作中很常见的需求,只需要一行代码就可以搞定: self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"back"] style:(UIBarButtonItemStyleDone) target:self action:@selector(backClick)]; 然后实现这个监听方法: - (void)backClick

UWP滑动手势

经过近些年智能手机App的不断发展,用户已经不仅仅满足于功能上的需求.UI.设计等非功能点逐渐在App体验中占了大多数的分数.不知从何时起,滑动手势就成为了App的一个标配.他不仅仅是一个功能,更是一个UI设计.其有以下几个优点: 1.方便了单手操作.在当今大屏手机占有率越来越高的趋势下,简洁方便的单手操作模式是很有必要的. 2.美化了UI.你可能会有疑问,这是个功能,为什么美化了UI呢?其实,手势操作的另一个利器就是隐藏了部分不是那么美观的UI,使得界面看上去更简.比如邮件App ,你如果要在

自定义导航栏返回时的滑动手势处理

现在使用默认模板创建的iOS App都支持手势返回功能,如果导航栏的返回按钮是自定义的那么则会失效,也可以参考这里手动设置无效. if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) { self.navigationController.interactivePopGestureRecognizer.enabled = NO; } 如果是因为自定义导航按钮而导

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

新手教程之使用Xib自定义UITableViewCell 前言 首先:什么是UITableView?看图 其次:什么是cell? 然后:为什么要自定cell,UITableView不是自带的有cell么? 因为在日常开发中,系统自带的cell满足不了客户和开发人员的需求(并且每个cell中的内容\大小\样式相同),我们就需要自定义cell来实现更加优化的功能.比如下面这种 最后:怎么自定义cell? 1.创建一个新的项目,在storyboard中拖入两个imageView,两个label   2

用xib自定义UITableViewCell

1.文件结构: 2. 先创建一个xib文件,删除原有的view,添加一个TableViewCell控件. 3.ModelTableViewController.m文件 1 #import "ModelTableViewController.h" 2 #import "Cell.h" 3 4 5 @interface ModelTableViewController () 6 7 @end 8 9 @implementation ModelTableViewContr

如何添加ZBLibrary中的底部左右滑动手势

我们可以查看一下以前版本的ASP.NET程序,它是没有Main()函数的,也就是说它没有程序入口点,不是单独的进程.对于应用程序开发来说,这个问题并不大,因为开发者在意的Web程序的逻辑.数据安全等问题,而不是应用程序如何被加载.但对于一个Web框架来说,这个问题非常严重,因为它高度依赖IIS和Windows Server,减少了它的适用范围.如果我们查看ASP.NET Core的程序,你会发现它本质上就是一个控制台程序,如果我们把那些在Main()函数中自动生成的代码都删掉(VS2015的模板

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

在自定义UITableViewCell中创建了一个按钮. 想在点击该按钮时知道该按钮所在的cell在TableView中的行数.就是cell的 indexPath.row 两种方法都很好.-(IBAction):(id)sender{    NSLog(@"MyRow:%d",[self.table indexPathForCell:((TableViewCell*)[[sender   superview]superview])].row); //这个方便一点点,不用设置tag.  

实际iOS编程中遇到的自定义导航栏按钮,导致手势返回失效的解决方法

1\在实际编程过程中往往需要自定义导航栏上面的按钮,也就用: - (instancetype)initWithCustomView:(UIView *)customView; 但用了这个方法后可能会导致iOS7,8的手势返回失效,解决方法就是在自定义的导航栏的viewDidLoad方法中添加如下代码 注意:只有用系统的导航栏,或者继承于系统的导航栏才可以用Push方法,并且自带返回手势. - (void)viewDidLoad { [super viewDidLoad]; __weak type