最近接手了一个项目,做一个视频下载的功能,视频下载完成后,如果想要删除某一条数据的话,左滑点击删除按钮删除,但是发现点击后没有效果,无法触发代理方法,检查了代理,查看图层是否有遮挡,都没有问题,如果按着cell滑动到最左边是可以触发的,找了两个多小时,也没发现问题出在哪,没办法,也许是自己能力有限,不知道前人做了什么操作(如果你读到这并且知道怎么解决,请在下面留言给我,感激不尽)现在只能换个思路解决问题,现在给这个页面加个全选的功能来代替这个事情,本来需要一个一个删除,现在可以多条一起删除了,哈哈😄
这个全选的功能还是很简单的
Now ,First,show you all my code
#import "ViewController.h"
@interface ViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArr; //当前页面的数据
@property (nonatomic, strong) NSMutableArray *deleteArr; //删除数据的数组
@property (nonatomic, strong) NSMutableArray *markArr; //标记数据的数组
@property (nonatomic, strong) UIButton *selectAllButton; //全选按钮
@property (nonatomic, strong) UIButton *deleteButton; //删除按钮
@end
@implementation ViewController
- (NSMutableArray *)dataArr {
if (_dataArr == nil) {
_dataArr = [NSMutableArray array];
for (int i = 0; i < 15; i++) {
[_dataArr addObject:[NSString stringWithFormat:@"数据%2d",i]];
}
}
return _dataArr;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.deleteArr = [NSMutableArray array];
self.markArr = [NSMutableArray array];
self.title = @"多选Demo";
self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-60) style:UITableViewStylePlain];
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.tableView.editing = NO;
[self.view addSubview:self.tableView];
//选择按钮
UIButton *selectedBtn = [UIButton buttonWithType:UIButtonTypeSystem];
selectedBtn.frame = CGRectMake(0, 0, 60, 30);
[selectedBtn setTitle:@"选择" forState:UIControlStateNormal];
[selectedBtn addTarget:self action:@selector(selectedBtn:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *selectItem = [[UIBarButtonItem alloc] initWithCustomView:selectedBtn];
self.navigationItem.rightBarButtonItem =selectItem;
//全选按钮
_selectAllButton = [UIButton buttonWithType:UIButtonTypeSystem];
_selectAllButton.frame = CGRectMake(0, 0, 60, 30);
[_selectAllButton setTitle:@"全选" forState:UIControlStateNormal];
[_selectAllButton addTarget:self action:@selector(selectAllBtnClick:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:_selectAllButton];
self.navigationItem.leftBarButtonItem = leftItem;
_selectAllButton.hidden = YES;
//删除按钮
_deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
_deleteButton.backgroundColor = [UIColor redColor];
[_deleteButton setTitle:@"删除" forState:UIControlStateNormal];
_deleteButton.frame = CGRectMake(0, self.view.frame.size.height- 60, self.view.frame.size.width, 60);
[_deleteButton addTarget:self action:@selector(deleteClick:) forControlEvents:UIControlEventTouchUpInside];
_deleteButton.enabled = NO;
[self.view addSubview:_deleteButton];
}
//删除按钮点击事件
- (void)deleteClick:(UIButton *)button {
if (self.tableView.editing) {
//删除
[self.dataArr removeObjectsInArray:self.deleteArr];
[self.tableView reloadData];
}
else return;
}
//选择按钮点击响应事件
- (void)selectedBtn:(UIButton *)button {
_deleteButton.enabled = YES;
//支持同时选中多行
self.tableView.allowsMultipleSelectionDuringEditing = YES;
self.tableView.editing = !self.tableView.editing;
if (self.tableView.editing) {
_selectAllButton.hidden = NO;
[button setTitle:@"完成" forState:UIControlStateNormal];
}else{
_selectAllButton.hidden = YES;
[button setTitle:@"选择" forState:UIControlStateNormal];
}
}
//全选
- (void)selectAllBtnClick:(UIButton *)button {
for (int i = 0; i < self.dataArr.count; i ++) {
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
[self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionTop];
[self.deleteArr addObjectsFromArray:self.dataArr];
}
NSLog(@"self.deleteArr:%@", self.deleteArr);
}
#pragma mark - UITableViewDelegate
//选择你要对表进行处理的方式 (默认是删除方式)
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
}
//选中时将选中行的在self.dataArr 中的数据添加到删除数组self.deleteArr中
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[self.deleteArr addObject:[self.dataArr objectAtIndex:indexPath.row]];
}
//取消选中时 将存放在self.deleteArr中的数据移除
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
[self.deleteArr removeObject:[self.dataArr objectAtIndex:indexPath.row]];
}
#pragma mark - UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.dataArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *ident = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ident];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ident];
}
cell.textLabel.text = [self.dataArr objectAtIndex:indexPath.row];
return cell;
}
@end
接下来讲解一下这个代码:这个功能还是很基础的,当然如果上面的代码你可以直接用,或者看着很简单,下面的就可以不用看,因为下面的对你来说可能太啰嗦 了
Now,let's begin:
首先,你需要声明一个数组用来存储需要删除的数据和再声明一个数组用来做标记
多选删除的功能主要需要在这几个代理方法里面操作:
在选中的代理方法里面,讲需要删除的cell添加到删除的数组中
在取消选中的数组中,将需要删除的cell从deleteArr数组中移除
#pragma mark - UITableViewDelegate
//选择你要对表进行处理的方式 (默认是删除方式)
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
}
//选中时将选中行的在self.dataArr 中的数据添加到删除数组self.deleteArr中
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[self.deleteArr addObject:[self.dataArr objectAtIndex:indexPath.row]];
}
//取消选中时 将存放在self.deleteArr中的数据移除
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
[self.deleteArr removeObject:[self.dataArr objectAtIndex:indexPath.row]];
}
选择按钮的点击相应事件
//选择按钮点击响应事件
- (void)selectedBtn:(UIButton *)button {
_deleteButton.enabled = YES;
//支持同时选中多行
self.tableView.allowsMultipleSelectionDuringEditing = YES;
self.tableView.editing = !self.tableView.editing;
if (self.tableView.editing) {
_selectAllButton.hidden = NO;
[button setTitle:@"完成" forState:UIControlStateNormal];
}else{
_selectAllButton.hidden = YES;
[button setTitle:@"选择" forState:UIControlStateNormal];
}
}
选择按钮的点击相应事件
//删除按钮点击事件
- (void)deleteClick:(UIButton *)button {
if (self.tableView.editing) {
//删除
[self.dataArr removeObjectsInArray:self.deleteArr];
[self.tableView reloadData];
}
else return;
}
全选按钮的点击相应事件
//全选
- (void)selectAllBtnClick:(UIButton *)button {
for (int i = 0; i < self.dataArr.count; i ++) {
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
[self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionTop];
[self.deleteArr addObjectsFromArray:self.dataArr];
}
NSLog(@"self.deleteArr:%@", self.deleteArr);
}
如果你想做成跟安卓一样,长按cell出发编辑的效果,可以在创建cell的时候添加下面这段代码
//长按手势
UILongPressGestureRecognizer *longPressed = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressedAct:)];
longPressed.minimumPressDuration = 1;
[cell.contentView addGestureRecognizer:longPressed];
-(void)longPressedAct:(UILongPressGestureRecognizer *)gesture
{
if(gesture.state == UIGestureRecognizerStateBegan) {
CGPoint point = [gesture locationInView:self.tableView];
NSIndexPath * indexPath = [self.tableView indexPathForRowAtPoint:point];
if(indexPath == nil) return ;
self.tableView.editing = YES;
}
}
结束:写的有点啰嗦,希望对你有点作用
网友评论