前言:
各位同学大家好 ,有段时间没有给大家更新文章了 。 具体多久我也记不清楚了,春节放假比较早,所以就趁着有时间学习了一下iOS开发的基础知识 今天讲的iOS UITableView 配和block 回调实现列表删除教程 。那么废话不多说 ,我们正式开始。
准备工作
安装xcode 这个大家可以自己去appstore 搜索下载安装即可
效果图
具体实现
- 列表显示
@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 回调实现列表删除教程就讲完了
最后总结
在iOS开发中这种列表展示基本都是使用 UITableView 和UICollectionView 我这边只是加单举例所以直接就用UITableView 实现了 我们要注意的是block回调使用和动画的处理 以及自定义cell的处理 就差不多了 ,我是一名安卓程序员 因为需要就来学习一下iOS的开发, 最近看了一下教程学习了IOS里面的一些基础控件的用法。 就想着做个笔记 所以就写了这篇文章 ,记录一下 如果有错误的请大神们指出。 最后希望我的文章能帮助到各位解决问题 ,以后我还会贡献更多有用的代码分享给大家。各位同学如果觉得文章还不错 ,麻烦给关注和star,小弟在这里谢过啦