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
沒有數(shù)據(jù)、初始化一個數(shù)組當假數(shù)據(jù)义锥!
// 數(shù)據(jù)
_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)];
}
//根據(jù)偏移量計算是否滑動到tableView最后一行 count為無符號整型 強轉一下
if (_tableView.contentOffset.y > 50.0000000*(NSInteger)(_cellArray.count - 12 - 2)) {
//cell高度*(模型數(shù)組個數(shù) - 頁面展示總cell數(shù) + 2) = 滑動到tableView倒數(shù)第二行的偏移量
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];
// 確定選擇哪條數(shù)據(jù)
for (NSIndexPath *indexPath in _tableView.indexPathsForSelectedRows) {
[deleteArr addObject:_cellArray[indexPath.row]];
}
// 從總數(shù)據(jù)刪除被選中的數(shù)據(jù)
[_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];
}
改變編輯狀態(tài)下左側圓圈勾選顏色泡孩!
cell.tintColor = [UIColor greenColor];// 改變編輯狀態(tài)左側勾選顏色
我的更多文章:你等下課滴
您可以關注我以便隨時查看我最新的文章,本篇文章是為了做筆記寺谤,順便提供給大家共同學習進步仑鸥!如果您對本篇文章有任何疑問吮播,請留言給我,有什么錯誤也可以留言提醒眼俊,如果對大家有幫助我很榮幸意狠!感謝!