tableview编辑删除、全选删除、左滑删除、滑动到底端自动向上移动view的距离保证不遮挡!
编辑按钮在导航栏右侧、底端一个自定义footerView、删除、全选!
#define VIEW_WIDTH [[UIScreen mainScreen] bounds].size.width
#define VIEW_HEIGHT [[UIScreen mainScreen] bounds].size.height
#import "ViewController.h"
#import "TableViewCell.h"
#import "FootView.h"
@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) FootView *footView;
@property (nonatomic, strong) NSMutableArray *cellArray;
@end
没有数据、初始化一个数组当假数据!
// 数据
_cellArray = [NSMutableArray arrayWithObjects:@"1", @"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9", @"10", @"11", @"12", @"13", @"14", @"15", @"16", @"17", @"18", @"19", @"20", @"21", @"22", @"23", @"24", @"25", @"26", @"27", @"28", @"29", @"30", @"31", @"32", @"33", @"34", @"35", nil];
// 编辑
- (void)changeNavRightItem{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(0, 0, 50, 30);
[btn setTitle:@"编辑" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:btn];
self.navigationItem.rightBarButtonItem = item;
}
编辑逻辑!
#pragma mark--Editing
- (void)btnClick:(UIButton *)btn{
btn.selected = !btn.selected;
if (btn.selected) {
_footView.hidden = NO;
[btn setTitle:@"取消" forState:UIControlStateNormal];
//防止当全部选择时,点击取消编辑,再次进入第一次点击全部选择会无效
[_footView.allBtn setTitle:@"全部选择" forState:UIControlStateNormal];
_footView.allBtn.selected = NO;
}else {
_footView.hidden = YES;
[btn setTitle:@"编辑" forState:UIControlStateNormal];
}
//让tableView进入编辑模式
[_tableView setEditing:!_tableView.isEditing animated:YES];
if (_tableView.isEditing) {
[_tableView setFrame:CGRectMake(0, 0, VIEW_WIDTH, VIEW_HEIGHT - 49)];
}else {
[_tableView setFrame:CGRectMake(0, 0, VIEW_WIDTH, VIEW_HEIGHT)];
}
//根据偏移量计算是否滑动到tableView最后一行 count为无符号整型 强转一下
if (_tableView.contentOffset.y > 50.0000000*(NSInteger)(_cellArray.count - 12 - 2)) {
//cell高度*(模型数组个数 - 页面展示总cell数 + 2) = 滑动到tableView倒数第二行的偏移量
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[_cellArray indexOfObject:[_cellArray lastObject]] inSection:0];
[_tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:NO];
}
}
删除逻辑!
#pragma mark--Delete
- (void)footDeleteBtnClick{
NSMutableArray *deleteArr = [[NSMutableArray alloc] init];
// 确定选择哪条数据
for (NSIndexPath *indexPath in _tableView.indexPathsForSelectedRows) {
[deleteArr addObject:_cellArray[indexPath.row]];
}
// 从总数据删除被选中的数据
[_cellArray removeObjectsInArray:deleteArr];
[_tableView reloadData];
//防止当全部选择时,点击删除,再次点击全部选择会无效
[_footView.allBtn setTitle:@"全部选择" forState:UIControlStateNormal];
_footView.allBtn.selected = NO;
}
全选逻辑!
#pragma mark--AllSelected
- (void)footAllSelectedBtnClick:(UIButton *)btn{
btn.selected = !btn.selected;
for (int i = 0; i < _cellArray.count; i++) {
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
if (btn.selected) {
[_tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionTop];
[btn setTitle:@"取消全选" forState:UIControlStateNormal];
}else {
[_tableView deselectRowAtIndexPath:indexPath animated:NO];
[btn setTitle:@"全部选择" forState:UIControlStateNormal];
}
}
}
初始化tableview有一个属性!
_tableView.allowsMultipleSelectionDuringEditing = YES;//设置tableView在编辑模式下可多选
左滑删除!
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
if (editingStyle == UITableViewCellEditingStyleDelete) {
[_cellArray removeObjectAtIndex:indexPath.row];
}
[_tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
改变编辑状态下左侧圆圈勾选颜色!
cell.tintColor = [UIColor greenColor];// 改变编辑状态左侧勾选颜色
我的更多文章:你等下课滴
您可以关注我以便随时查看我最新的文章,本篇文章是为了做笔记,顺便提供给大家共同学习进步!如果您对本篇文章有任何疑问,请留言给我,有什么错误也可以留言提醒,如果对大家有帮助我很荣幸!感谢!