#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