//
// 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)系作者
- 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來炕倘,“玉大人钧大,你說我怎么就攤上這事≌中” “怎么了啊央?”我有些...
- 文/不壞的土叔 我叫張陵,是天一觀的道長涨醋。 經(jīng)常有香客問我瓜饥,道長,這世上最難降的妖魔是什么浴骂? 我笑而不...
- 正文 為了忘掉前任乓土,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘趣苏。我一直安慰自己狡相,他們只是感情好,可當(dāng)我...
- 文/花漫 我一把揭開白布食磕。 她就那樣靜靜地躺著尽棕,像睡著了一般。 火紅的嫁衣襯著肌膚如雪彬伦。 梳的紋絲不亂的頭發(fā)上滔悉,一...
- 文/蒼蘭香墨 我猛地睜開眼份氧,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了弯屈?” 一聲冷哼從身側(cè)響起蜗帜,我...
- 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎资厉,沒想到半個月后厅缺,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
- 正文 獨(dú)居荒郊野嶺守林人離奇死亡宴偿,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
- 正文 我和宋清朗相戀三年湘捎,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片窄刘。...
- 正文 年R本政府宣布翻伺,位于F島的核電站材泄,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏吨岭。R本人自食惡果不足惜拉宗,卻給世界環(huán)境...
- 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧旦事,春花似錦魁巩、人聲如沸。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至单料,卻和暖如春埋凯,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背扫尖。 一陣腳步聲響...
- 正文 我出身青樓甩恼,卻偏偏與公主長得像,于是被迫代替她去往敵國和親沉颂。 傳聞我的和親對象是個殘疾皇子条摸,可洞房花燭夜當(dāng)晚...
推薦閱讀更多精彩內(nèi)容
- 開發(fā)過程中或多或少都會遇到tableview的各種功能彻坛,這里簡單記錄一下tableview的刪除和全選刪除功能顷啼,廢...
- 在以往的慣性思維中,對于批量刪除的操作一直以為是標(biāo)記被選中的Cell 控件進(jìn)行刪除昌屉,但往往前面被選中的Cell會被...
- 我們主動找對方說話的一方,往往叫做溝通的主動方抢呆,主動發(fā)起的一場溝通煮嫌,我們就叫做“溝通邀請”。 任何希望和對方能夠建...