1自定義tableview的刪除
近來有個需求,自定義tableview的滑動刪除闯传,查看了很多資料峰髓,沒有找到合適的,就自己研究了下完沪,這個方法可以根據(jù)你的需求任意的修改
2下面就說下思路
該需求是通過代理實(shí)現(xiàn)
在tableviewcell里面創(chuàng)建你需要的按鈕泡躯,像下面的代碼一樣添加到self.contentView上
[self.contentView addSubview:deleteBtn];
然后聲明一個uiview,然后把這個view添加到self.contentView上,最后把這個view放到視圖層的最上面即可
@property(nonatomic,weak)UIView *containerView;
UIView *containerView = [[UIView alloc] init];
[self.contentView addSubview:containerView];
self.containerView = containerView;
[self.contentView bringSubviewToFront:self.containerView]; //設(shè)置containerView顯示在最上層
這時你要注意丽焊,你要把你想在cell上顯示的東西较剃,創(chuàng)建后,是添加到self.containerView上的
下面就是添加滑動手勢技健,添加了左右滑動手勢写穴,添加左右手勢的好處是,當(dāng)滑動這個時雌贱,另一個會關(guān)閉滑動
//3啊送、給容器containerView綁定左右滑動清掃手勢
UISwipeGestureRecognizer *leftSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
leftSwipe.direction = UISwipeGestureRecognizerDirectionLeft; //設(shè)置向左清掃
[self.containerView addGestureRecognizer:leftSwipe];
UISwipeGestureRecognizer *rightSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
rightSwipe.direction = UISwipeGestureRecognizerDirectionRight;//設(shè)置向右清掃
[self.containerView addGestureRecognizer:rightSwipe];
以上這些是UI界面上的實(shí)現(xiàn)
下面說些cell里面的東西
重寫cell的構(gòu)建方法
.h
//靜態(tài)構(gòu)造方法
+(instancetype)cellWithTableView:(UITableView *)tableview;
.m
+(instancetype)cellWithTableView:(UITableView *)tableview
{
static NSString *reuseIdentity=@"cell";
ZYJ_TableViewCell *cell = [tableview dequeueReusableCellWithIdentifier:reuseIdentity];
if (cell==nil) {
cell=[[ZYJ_TableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentity];
}
//cell.backgroundColor=[UIColor redColor];
return cell;
}
然后重寫cell的類型方法
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self=[super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
[self createUI];
}
return self;
}
以上就是在tableviewcell里面實(shí)現(xiàn)的主要代碼
下面是怎么在controller里面實(shí)現(xiàn)
在controller里面創(chuàng)建tableview,和以往是一樣的欣孤,不同點(diǎn)是在下面的這個函數(shù)里面
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
因?yàn)樵赾ell里面已經(jīng)重寫了cell的構(gòu)建方法馋没,在該函數(shù)里面調(diào)用就行
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
ZYJ_TableViewCell *cell=[ZYJ_TableViewCell cellWithTableView:tableView];
cell.delegate=self;//代理
ZYJ_Model *model=self.dataArry[indexPath.row];
NSLog(@"%@",model);
//cell.backgroundColor=[UIColor brownColor];
[cell showdataWith:model];
return cell;
}
然后實(shí)現(xiàn)cell里面的代理方法即可
-(void)deleteCell:(ZYJ_TableViewCell *)cell
{
NSIndexPath *path=[self.ZYJ_Tableview indexPathForCell:cell];
[self.dataArry removeObjectAtIndex:path.row];
[self.ZYJ_Tableview deleteRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationLeft];
}
-(void)closeOtherCellLeftSwipe
{
//循環(huán)顯示的cell
for (ZYJ_TableViewCell *item in self.ZYJ_Tableview.visibleCells) {
[item closeSwipe];
}
}
以上就是自定義tableview滑動刪除的主要思路和主要代碼
下面是代碼tableviewcell里面的代碼
@class ZYJ_TableViewCell;
@protocol CellButtonEventViewDelegate <NSObject>
@optional
-(void)deleteCell:(ZYJ_TableViewCell *)cell; //刪除cell
- (void)closeOtherCellLeftSwipe; //關(guān)閉其他單元格的左滑
@end
@interface ZYJ_TableViewCell : UITableViewCell
//靜態(tài)構(gòu)造方法
+(instancetype)cellWithTableView:(UITableView *)tableview;
@property(nonatomic,strong)ZYJ_Model *model;
-(void)showdataWith:(ZYJ_Model *)model;
- (void)closeSwipe; //關(guān)閉滑動,恢復(fù)原樣(用于在滑動當(dāng)前單元格時降传,把其他已經(jīng)左滑的單元格關(guān)閉)
@property(nonatomic) BOOL isSwipMode;//是否滑動
@property (nonatomic,weak) id <CellButtonEventViewDelegate> delegate;
@end
@interface ZYJ_TableViewCell()
@property(nonatomic,weak)UIView *containerView;
@property(nonatomic,weak)UILabel *showlable;
@property(nonatomic,weak)UIButton *deleteBtn;
@property(nonatomic,weak)UIView *underView;
@property(nonatomic) BOOL isOpenLeft ;//是否已經(jīng)打開左滑動
@end
@implementation ZYJ_TableViewCell
- (void)awakeFromNib {
// Initialization code
}
+(instancetype)cellWithTableView:(UITableView *)tableview
{
static NSString *reuseIdentity=@"cell";
ZYJ_TableViewCell *cell = [tableview dequeueReusableCellWithIdentifier:reuseIdentity];
if (cell==nil) {
cell=[[ZYJ_TableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentity];
}
//cell.backgroundColor=[UIColor redColor];
return cell;
}
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self=[super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
[self createUI];
}
return self;
}
//初始化界面
-(void)createUI
{
UIButton *deleteBtn =[UIButton buttonWithType:UIButtonTypeCustom];
[self.contentView addSubview:deleteBtn];
self.deleteBtn=deleteBtn;
self.deleteBtn.backgroundColor=[UIColor redColor];
[self.deleteBtn setTitle:@"刪除" forState:UIControlStateNormal];
[self.deleteBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[self.deleteBtn addTarget:self action:@selector(deleteCell:) forControlEvents:UIControlEventTouchUpInside];
UIView *containerView = [[UIView alloc] init];
[self.contentView addSubview:containerView];
self.containerView = containerView;
self.containerView.backgroundColor=[UIColor orangeColor];
self.containerView.backgroundColor = [UIColor whiteColor];
UILabel *showLable=[[UILabel alloc]init];
[self.containerView addSubview:showLable];
self.showlable=showLable;
self.showlable.textColor=[UIColor purpleColor];
//self.showlable.backgroundColor=[UIColor redColor];
UIView *underView=[[UIView alloc]init];
[self.containerView addSubview:underView];
self.underView=underView;
self.underView.backgroundColor=[UIColor purpleColor];
//3篷朵、給容器containerView綁定左右滑動清掃手勢
UISwipeGestureRecognizer *leftSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
leftSwipe.direction = UISwipeGestureRecognizerDirectionLeft; //設(shè)置向左清掃
[self.containerView addGestureRecognizer:leftSwipe];
UISwipeGestureRecognizer *rightSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
rightSwipe.direction = UISwipeGestureRecognizerDirectionRight;//設(shè)置向右清掃
[self.containerView addGestureRecognizer:rightSwipe];
self.selectionStyle = UITableViewCellSelectionStyleNone; //設(shè)置單元格選中樣式
[self.contentView bringSubviewToFront:self.containerView]; //設(shè)置containerView顯示在最上層
}
//子控件的布局
-(void)layoutSubviews
{
self.containerView.frame=self.contentView.bounds;
self.deleteBtn.frame=CGRectMake(self.frame.size.width*0.8, 10, self.frame.size.width*0.2, 40);
self.showlable.frame=CGRectMake(0, 0, 200, 30);
self.underView.frame=CGRectMake(15, 59, self.frame.size.width-15, 1);
}
//設(shè)置要顯示的時間
-(void)showdataWith:(ZYJ_Model *)model
{
_model=self.model;
self.showlable.text=model.title;
NSLog(@"%@",model.title);
}
-(void)deleteCell:(UIButton *)btn
{
if ([self.delegate respondsToSelector:@selector(deleteCell:)]) {
[self.delegate deleteCell:self];
}
}
//滑動手勢
-(void)swipe:(UISwipeGestureRecognizer *)swipe
{
if (swipe.direction == UISwipeGestureRecognizerDirectionLeft){
if (self.isOpenLeft) return; //已經(jīng)打開左滑,不再執(zhí)行
//開始左滑: 先調(diào)用代理關(guān)閉其他cell的左滑
if ([self.delegate respondsToSelector:@selector(closeOtherCellLeftSwipe)])
[self.delegate closeOtherCellLeftSwipe];
[UIView animateWithDuration:0.5 animations:^{
swipe.view.center = CGPointMake(0, self.frame.size.height/2);
self.containerView.frame=CGRectMake(-self.frame.size.width*0.2, 0, self.frame.size.width, 60.0f);
}];
self.isOpenLeft = YES;
}
else if (swipe.direction == UISwipeGestureRecognizerDirectionRight){
[self closeSwipe]; //關(guān)閉左滑
}
}
-(void)closeSwipe
{
if (!self.isOpenLeft) return; //還未打開左滑婆排,不需要執(zhí)行右滑
[UIView animateWithDuration:0.5 animations:^{
self.containerView.center = CGPointMake(self.frame.size.width/2 , self.frame.size.height/2 );
}];
self.isOpenLeft = NO;
}