//
// ViewController.m
// tableViewDemo
//
// Created by qianfeng on 16/11/16.
// Copyright ? 2016年 qianfeng. All rights reserved.
//
#import "ViewController.h"
#import "Cell.h"
static NSString *cellID = @"Cell";
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
{
BOOL isEditBtnSelected;
BOOL isSelectBtnSelected;
}
@property(strong,nonatomic) UITableView *tableView;
@property(strong,nonatomic) NSMutableArray *dataSource;
@property(strong,nonatomic) UIButton *editBtn;
@property(strong,nonatomic) UIButton *selectBtn;
@end
@implementation ViewController
-(NSMutableArray *)dataSource{
if (!_dataSource) {
_dataSource = [NSMutableArray array];
}
return _dataSource;
}
- (void)viewDidLoad {
[super viewDidLoad];
isEditBtnSelected = NO;
isSelectBtnSelected = NO;
self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
[self.view addSubview:self.tableView];
self.tableView.allowsMultipleSelectionDuringEditing = YES;
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.tableView.rowHeight = 60;
//注冊
[self.tableView registerNib:[UINib nibWithNibName:cellID bundle:nil] forCellReuseIdentifier:cellID];
//編輯
_editBtn = [UIButton buttonWithType:UIButtonTypeSystem];
[_editBtn addTarget:self action:@selector(editBtnClick:) forControlEvents:UIControlEventTouchUpInside];
_editBtn.frame = CGRectMake(0, 0, 80, 30);
[_editBtn setTitle:@"編輯" forState:UIControlStateNormal];
[_editBtn setTitle:@"取消編輯" forState:UIControlStateSelected];
UIBarButtonItem *rightEditItem = [[UIBarButtonItem alloc]initWithCustomView:_editBtn];
//全選
_selectBtn = [UIButton buttonWithType:UIButtonTypeSystem];
[_selectBtn addTarget:self action:@selector(selectBtnClick:) forControlEvents:UIControlEventTouchUpInside];
_selectBtn.frame = CGRectMake(0, 0, 80, 30);
[_selectBtn setTitle:@"全選" forState:UIControlStateNormal];
[_selectBtn setTitle:@"取消全選" forState:UIControlStateSelected];
UIBarButtonItem *rightSelectItem = [[UIBarButtonItem alloc]initWithCustomView:_selectBtn];
self.navigationItem.rightBarButtonItems = @[rightEditItem,rightSelectItem];
//刪除
UIBarButtonItem *deleteBtn = [[UIBarButtonItem alloc]initWithTitle:@"刪除" style:UIBarButtonItemStylePlain target:self action:@selector(deleteBtnClick:)];
self.navigationItem.leftBarButtonItem = deleteBtn;
[self.dataSource addObject:@"xxx"];
[self.dataSource addObject:@"xxx"];
[self.dataSource addObject:@"xxx"];
[self.dataSource addObject:@"xxx"];
[self.dataSource addObject:@"xxx"];
[self.dataSource addObject:@"xxx"];
[self.dataSource addObject:@"xxx"];
[self.dataSource addObject:@"xxx"];
[self.dataSource addObject:@"xxx"];
[self.dataSource addObject:@"xxx"];
[self.dataSource addObject:@"xxx"];
[self.dataSource addObject:@"xxx"];
[self.dataSource addObject:@"xxx"];
[self.dataSource addObject:@"xxx"];
[self.dataSource addObject:@"xxx"];
[self.dataSource addObject:@"xxx"];
[self.dataSource addObject:@"xxx"];
[self.dataSource addObject:@"xxx"];
[self.dataSource addObject:@"xxx"];
[self.dataSource addObject:@"xxx"];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.dataSource.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
Cell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
return cell;
}
//編輯
-(void)editBtnClick:(id)sender
{
isEditBtnSelected = !isEditBtnSelected;
self.editBtn.selected = isEditBtnSelected;
if (isEditBtnSelected) {
self.tableView.editing = YES;
}else{
self.tableView.editing = NO;
}
}
//全選
-(void)selectBtnClick:(id)sender
{
isSelectBtnSelected = !isSelectBtnSelected;
self.selectBtn.selected = isSelectBtnSelected;
if (isSelectBtnSelected) {
for (NSInteger i = 0; i < self.dataSource.count; i ++) {
NSIndexPath *indexpath = [NSIndexPath indexPathForRow:i inSection:0];
[self.tableView selectRowAtIndexPath:indexpath animated:YES scrollPosition:0];
}
}else{
for (NSInteger i = 0; i < self.dataSource.count; i ++) {
NSIndexPath *indexpath = [NSIndexPath indexPathForRow:i inSection:0];
[self.tableView deselectRowAtIndexPath:indexpath animated:YES];
}
}
}
// 刪除
-(void)deleteBtnClick:(id)sender
{
//獲取所有
NSArray *indexpaths = [self.tableView indexPathsForSelectedRows];
NSMutableIndexSet *indexSet = [NSMutableIndexSet indexSet];
for (NSIndexPath *indexpath in indexpaths) {
[indexSet addIndex:indexpath.row];
}
[self.dataSource removeObjectsAtIndexes:indexSet];
[self.tableView deleteRowsAtIndexPaths:indexpaths withRowAnimation:UITableViewRowAnimationBottom];
}
@end
tableview--編輯全選刪除
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
相關(guān)閱讀更多精彩內(nèi)容
- 開發(fā)過程中或多或少都會遇到tableview的各種功能,這里簡單記錄一下tableview的刪除和全選刪除功能,廢...
- 在以往的慣性思維中,對于批量刪除的操作一直以為是標(biāo)記被選中的Cell 控件進(jìn)行刪除,但往往前面被選中的Cell會被...
- 我們主動找對方說話的一方,往往叫做溝通的主動方,主動發(fā)起的一場溝通,我們就叫做“溝通邀請”。 任何希望和對方能夠建...