iOS UITableView配合block 回调实现列表删除教程
发布时间:2023-04-06 11:45:50 所属栏目:教程 来源:
导读:各位同学大家好 ,有段时间没有给大家更新文章了 。 具体多久我也记不清楚了,春节放假比较早,所以就趁着有时间学习了一下iOS开发的基础知识 今天讲的iOS UITableView 配和block 回调实现列表删除教程 。那么废话不
|
各位同学大家好 ,有段时间没有给大家更新文章了 。 具体多久我也记不清楚了,春节放假比较早,所以就趁着有时间学习了一下iOS开发的基础知识 今天讲的iOS UITableView 配和block 回调实现列表删除教程 。那么废话不多说 ,我们正式开始。 准备工作 安装xcode 这个大家可以自己去appstore 搜索下载安装即可 效果图 QQ20210210-160354-HD[00_00_00--00_00_08].gif 具体实现 列表显示 @interface ViewController ()<UITableViewDelegate,UITableViewDataSource,GTNotmalTableViewDelegate> @property(nonatomic,strong,readwrite)UITableView * tableview; @property(nonatomic,strong,readwrite)NSMutableArray * dataArray; @end @implementation ViewController - (instancetype)init{ self= [super init]; if(self){ _dataArray=@[].mutablecopy; for(int i=0; i<20;i++){ [_dataArray addobject:@(i)]; } } return self; } - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor= [UIColor whiteColor]; _tableview= [[UITableView alloc]initWithFrame:self.view.bounds]; _tableview.dataSource=self; _tableview.delegate=self; [self.view addSubview:_tableview]; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return 110 ; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ NSLog(@"%ld", indexPath.row) } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return _dataArray.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ //UItableview cell 复用机制 GTnormalTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"id"]; if(!cell){ cell =[[GTnormalTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"id"]; cell.deledate=self; } [cell layoutTableViewCell]; return cell; } - (void) tableviewCell:(UITableViewCell *)tabviewCell clickDeleteButton:(UIButton * )deletebutton{ NSLog(@"clickDeleteButton"); GTDeleteCellView * deleteview =[[GTDeleteCellView alloc] initWithFrame:self.view.bounds]; CGRect rect =[tabviewCell convertRect:deletebutton.frame toView:nil]; __weak typeof(self)wself= self; [deleteview showDeleteViewFromPoint:rect.origin clickBlock:^{ __strong typeof (self)strongSelf=wself; [strongSelf.dataArray removeLastObject]; NSLog(@"clickBlock 回调"); [self.tableview deleteRowsAtIndexPaths:@[[strongSelf.tableview indexPathForCell:tabviewCell]] withRowAnimation:UITableViewRowAnimationAutomatic]; } ]; } @end 我们这边调用系统的 UITableView 组件 实例化 然后添加自定义的cell 来实现 整个列表的展示 添加自定义的cell - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ //UItableview cell 复用机制 GTnormalTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"id"]; if(!cell){ cell =[[GTnormalTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"id"]; cell.deledate=self; } [cell layoutTableViewCell]; return cell; } 自定义 cell 内容 // // GTnormalTableViewCell.m // shop // // Created by xuqing on 2021/1/27. // copyright © 2021 xuqing. All rights reserved. // #import "GTnormalTableViewCell.h" @interface GTnormalTableViewCell() @property(nonatomic,strong,readwrite)UILabel * titleLable; @property(nonatomic,strong,readwrite)UILabel * sourceLable; @property(nonatomic,strong,readwrite)UILabel * commentLable; @property(nonatomic,strong,readwrite)UILabel * timeLable; @property(nonatomic,strong,readwrite)UIImageView *richtimageview; @property(nonatomic,strong,readwrite)UIButton* deletebutton; @end @implementation GTnormalTableViewCell - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(nullable Nsstring *) reuseIdentifier { self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]; if(self){ [self.contentView addSubview:({ self.titleLable=[[UILabel alloc]initWithFrame:CGRectMake(20, 15, 300, 50)]; // self.titleLable.backgroundColor=[UIColor redColor]; self.titleLable; })]; [self.contentView addSubview:({ self.sourceLable=[[UILabel alloc]initWithFrame:CGRectMake(20, 80, 50, 20)]; // self.sourceLable.backgroundColor=[UIColor redColor]; self.sourceLable; })]; [self.contentView addSubview:({ self.commentLable=[[UILabel alloc]initWithFrame:CGRectMake(100 ,80, 50, 20)]; // self.commentLable.backgroundColor=[UIColor redColor]; self.commentLable; })]; [self.contentView addSubview:({ self.timeLable=[[UILabel alloc]initWithFrame:CGRectMake(150, 80, 50, 20)]; // self.timeLable.backgroundColor=[UIColor redColor]; self.timeLable; })]; [self.contentView addSubview:({ self.richtimageview=[[UIImageView alloc]initWithFrame:CGRectMake(330, 15, 70, 70)]; self.richtimageview.backgroundColor=[UIColor redColor]; self.richtimageview; })]; [self.contentView addSubview:({ self.deletebutton=[[UIButton alloc]initWithFrame:CGRectMake(290, 80, 30,20)]; self.deletebutton.backgroundColor=[UIColor blueColor]; [self.deletebutton setTitle:@"X" forState:UIControlStatenormal]; [self.deletebutton setTitle:@"V" forState:UIControlStateHighlighted]; [self.deletebutton addTarget:self action:@selector(deletebuttonClick) forControlEvents:UIControlEventTouchUpInside]; self.deletebutton; })]; } return self; } - (void)layoutTableViewCell{ self.titleLable.text=@"极客时间iOS开发"; self.sourceLable.text=@"极客时间"; [self.sourceLable sizetoFit]; self.commentLable.text=@"1999评论"; [self.commentLable sizetoFit]; self.commentLable.frame= CGRectMake(self.sourceLable.frame.origin.x+self.commentLable.frame.size.width+15, self.commentLable.frame.origin.y, self.commentLable.frame.size.width, self.commentLable.frame.size.height); self.timeLable.text=@"三分钟前"; [self.timeLable sizetoFit]; self.timeLable.frame= CGRectMake(self.commentLable.frame.origin.x+self.commentLable.frame.size.width+15, self.timeLable.frame.origin.y, self.timeLable.frame.size.width, self.timeLable.frame.size.height); } - (void)deletebuttonClick{ NSLog(@"deletebuttonClick"); if(self.deledate &&[self.deledate respondsToSelector:@selector(tableviewCell:clickDeleteButton:)]){ [self.deledate tableviewCell:self clickDeleteButton:self.deletebutton]; } } @end 为我们的删除按钮添加点击onClick 事件 [self.deletebutton addTarget:self action:@selector(deletebuttonClick) forControlEvents:UIControlEventTouchUpInside]; 具体点击事件 - (void)deletebuttonClick{ NSLog(@"deletebuttonClick"); if(self.deledate &&[self.deledate respondsToSelector:@selector(tableviewCell:clickDeleteButton:)]){ [self.deledate tableviewCell:self clickDeleteButton:self.deletebutton]; } } 我们在cell子布局里面的删除按钮的点击事件调用了 clickDeleteButton 方法 @protocol GTNotmalTableViewDelegate <NSObject> - (void) tableviewCell:(UITableViewCell *)tabviewCell clickDeleteButton:(UIButton * )deletebutton; @end 然后 我们在ViewController 中实现clickDeleteButton 方法 - (void) tableviewCell:(UITableViewCell *)tabviewCell clickDeleteButton:(UIButton * )deletebutton{ NSLog(@"clickDeleteButton"); GTDeleteCellView * deleteview =[[GTDeleteCellView alloc] initWithFrame:self.view.bounds]; CGRect rect =[tabviewCell convertRect:deletebutton.frame toView:nil]; __weak typeof(self)wself= self; [deleteview showDeleteViewFromPoint:rect.origin clickBlock:^{ __strong typeof (self)strongSelf=wself; [strongSelf.dataArray removeLastObject]; NSLog(@"clickBlock 回调"); [self.tableview deleteRowsAtIndexPaths:@[[strongSelf.tableview indexPathForCell:tabviewCell]] withRowAnimation:UITableViewRowAnimationAutomatic]; } ]; } 我们在 clickDeleteButton 方法中初始化GTDeleteCellView 并显示 弹出 GTDeleteCellView 具体实现 - (instancetype) initWithFrame:(CGRect)frame{ self= [super initWithFrame:frame]; if(self){ [self addSubview:({ _backgroundView=[[UIView alloc]initWithFrame:self.bounds]; _backgroundView.backgroundColor=[UIColor blackColor]; _backgroundView.alpha=0.5; [_backgroundView addGestureRecognizer:({ UITapGestureRecognizer * tapGesture= [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dismissDeleteView)]; tapGesture; })]; _backgroundView; })]; [self addSubview:({ _deleteButton=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 100, 50)]; _deleteButton.titleLabel.textColor = [UIColor whiteColor]; _deleteButton.titleLabel.text=@"删除"; [_deleteButton addTarget:self action:@selector(_clickButton) forControlEvents:UIControlEventTouchUpInside]; _deleteButton.backgroundColor=[UIColor redColor]; _deleteButton; })]; } return self; } GTDeleteCellView 的显示和关闭 显示 -(void)showDeleteViewFromPoint:(CGPoint)point clickBlock:(dispatch_block_t)clickBlock{ _deleteButton.frame=CGRectMake(point.x, point.y, 0, 0); _deleteBlock=[clickBlock copy]; UIWindow *window = [[[UIApplication sharedApplication] windows] objectAtIndex:0]; [window addSubview:self]; // [[UIApplication sharedApplication].keyWindow addSubview:self]; // [UIView animateWithDuration:1.f animations:^{ // self.deleteButton.frame=CGRectMake((self.bounds.size.width-200)/2 , (self.bounds.size.height-200)/2, 200, 200); // }]; // [UIView animateWithDuration:1.f delay:0.f usingSpringWithdamping:0.5 initialSpringVeLocity:0.5 options:UIViewAnimationCurveEaseInOut animations:^{ self.deleteButton.frame=CGRectMake((self.bounds.size.width-200)/2 , (self.bounds.size.height-200)/2, 200, 200); }completion:^(BOOL finished){ NSLog(@"completion"); }]; } showDeleteViewFromPoint 方法需要外部传入 CGPoint 和block 的回调实现 然后showDeleteViewFromPoint 方法里面我们处理 GTDeleteCellView 弹出的的动画效果 动画 [UIView animateWithDuration:1.f delay:0.f usingSpringWithdamping:0.5 initialSpringVeLocity:0.5 options:UIViewAnimationCurveEaseInOut animations:^{ self.deleteButton.frame=CGRectMake((self.bounds.size.width-200)/2 , (self.bounds.size.height-200)/2, 200, 200); }completion:^(BOOL finished){ NSLog(@"completion"); }]; 实例化block 和point _deleteButton.frame=CGRectMake(point.x, point.y, 0, 0); _deleteBlock=[clickBlock copy]; UIWindow *window = [[[UIApplication sharedApplication] windows] objectAtIndex:0]; [window addSubview:self]; 关闭 -(void)dismissDeleteView{ [self removeFromSuperview]; } 这是点空白的地方关闭 我们给deletebutton 添加点击事件 [_deleteButton addTarget:self action:@selector(_clickButton) forControlEvents:UIControlEventTouchUpInside]; 我们在点击事件方法实现里面 调用 block和 removeFromSuperview -(void)_clickButton{ // _deleteBlock(); if(_deleteBlock){ _deleteBlock(); NSLog(@"block回调执行"); } [self removeFromSuperview]; } 这样我们的deletebutton点击就也会关闭 GTDeleteCellView 然后我们在 ViewController 中将block 的会回调实现 并且删除dataArray数组里面的数据更新列表 CGRect rect =[tabviewCell convertRect:deletebutton.frame toView:nil]; __weak typeof(self)wself= self; [deleteview showDeleteViewFromPoint:rect.origin clickBlock:^{ __strong typeof (self)strongSelf=wself; [strongSelf.dataArray removeLastObject]; NSLog(@"clickBlock 回调"); [self.tableview deleteRowsAtIndexPaths:@[[strongSelf.tableview indexPathForCell:tabviewCell]] withRowAnimation:UITableViewRowAnimationAutomatic]; } ]; 我们调用 GTDeleteCellView 中的showDeleteViewFromPoint 方法传入我们转换后的point 和block 的实现 最后我们调用 [strongSelf.dataArray removeLastObject]; 删除数组里面的数据 然后调用 deleteRowsAtIndexPaths删除并刷新列表 [self.tableview deleteRowsAtIndexPaths:@[[strongSelf.tableview indexPathForCell:tabviewCell]] withRowAnimation:UITableViewRowAnimationAutomatic]; 到此iOS UITableView配合block 回调实现列表删除教程就讲完了 (编辑:汽车网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
