iOS tableviewCell的多行選擇刪除和全選刪除

記錄一下項目中遇到的tableviewCell多行選擇刪除和全選刪除爵赵。

效果圖如上圖

1 首先 創(chuàng)建數(shù)組

2 創(chuàng)建tableview?

self.tableView.editing = NO;//默認(rèn)tableview的editing 是不開啟的

3 全選和多選 刪除按鈕

//選擇按鈕

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;

//? ? ? ? 全選

_selectAllBtn = [UIButton buttonWithType:UIButtonTypeSystem];

_selectAllBtn.frame = CGRectMake(0, 0, 60, 30);

[_selectAllBtn setTitle:@"全選" forState:UIControlStateNormal];

[_selectAllBtn addTarget:self action:@selector(selectAllBtnClick:) forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:_selectAllBtn];

self.navigationItem.leftBarButtonItem = leftItem;

_selectAllBtn.hidden = YES;

_baseView = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height- 60, self.view.frame.size.width, 60)];

_baseView.backgroundColor = [UIColor grayColor];

[self.view addSubview:_baseView];

//刪除按鈕

_deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];

_deleteBtn.backgroundColor = [UIColor redColor];

[_deleteBtn setTitle:@"刪除" forState:UIControlStateNormal];

_deleteBtn.frame = CGRectMake(0, 0, self.view.frame.size.width, 60);

[_deleteBtn addTarget:self action:@selector(deleteClick:) forControlEvents:UIControlEventTouchUpInside];

[_baseView addSubview:_deleteBtn];

4 tableview的delegate和datasource

//是否可以編輯? 默認(rèn)的時YES

-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{

return YES;

}

//選擇編輯的方式,按照選擇的方式對表進(jìn)行處理

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{

if (editingStyle == UITableViewCellEditingStyleDelete) {//刪除

//真正項目中做刪除

//1.將表中的cell刪除

//2.將本地的數(shù)組中數(shù)據(jù)刪除

//3.最后將服務(wù)器端的數(shù)據(jù)刪除

}

}

//選擇你要對表進(jìn)行處理的方式? 默認(rèn)是刪除方式

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{

return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;

}

******//選中時將選中行的在self.dataArray 中的數(shù)據(jù)添加到刪除數(shù)組self.deleteArr中

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

[self.deleteArr addObject:[self.dataArray objectAtIndex:indexPath.row]];

}

******//取消選中時 將存放在self.deleteArr中的數(shù)據(jù)移除

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath? {

[self.deleteArr removeObject:[self.dataArray objectAtIndex:indexPath.row]];

}

實現(xiàn)完tableview的代理方法 下面處理各個按鈕的響應(yīng)事件

1 首先是選擇按鈕的響應(yīng)事件 在按鈕事件里面要有self.tableView.allowsMultipleSelectionDuringEditing = YES; 允許支持同時選中多行

然后在點擊的時候讓tableview.editing = !tableview.editing 點擊此按鈕可切換tableview的編輯狀態(tài)

- (void)selectedBtn:(UIButton *)button {

//支持同時選中多行

self.tableView.allowsMultipleSelectionDuringEditing = YES;

self.tableView.editing = !self.tableView.editing;

if (self.tableView.editing) {

_selectAllBtn.hidden = NO;

[button setTitle:@"完成" forState:UIControlStateNormal];

}else{

_selectAllBtn.hidden = YES;

[button setTitle:@"刪除" forState:UIControlStateNormal];

}

}


2 全選按鈕的響應(yīng)事件

點擊全選按鈕時 要在這里選中所有的cell 我在網(wǎng)上看到很多資料都是選中當(dāng)前可見的cell 我們項目要求是全部cell 频伤,所以在這里我這樣去做 在self.dataArray里面遍歷, 然后找到對應(yīng)的一共多少行恋追, 獲取索引值 indexPath唉铜,tableview有系統(tǒng)方法 [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionTop]; 選擇全部的cell ?此時在這個全選方法中將數(shù)據(jù)源self.dataArray的所有數(shù)據(jù)全部添加到self.deleteArr (存儲刪除數(shù)據(jù)的數(shù)組中)

//全選

- (void)selectAllBtnClick:(UIButton *)button {

//? ? [self.tableView reloadData];

for (int i = 0; i < self.dataArray.count; i ++) {

NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];

[self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionTop];

[self.deleteArr addObjectsFromArray:self.dataArray];

}

NSLog(@"self.deleteArr:%@", self.deleteArr);

}

3 刪除按鈕的處理事件

無論是多行刪除還是全部刪除的數(shù)據(jù)對tableview的操作都是走這個delete方法的喊崖。在方法中判斷當(dāng)前的tableview是否處于編輯狀態(tài)饲化。然后再執(zhí)行刪除操作,關(guān)鍵點就是將數(shù)據(jù)源self.dataArray中的要刪除的數(shù)據(jù)移除金蜀,之前我們的多選或者全選已經(jīng)將我們要刪除的數(shù)據(jù)存儲在self.deleteArr中了 刷后,所以在這里我們用[self.dataArray removeObjectsInArray:self.deleteArr];這個方法操作 然后刷新tableview。至此就可以實現(xiàn)功能了渊抄。


- (void)deleteClick:(UIButton *) button {

if (self.tableView.editing) {

//刪除

[self.dataArray removeObjectsInArray:self.deleteArr];

[self.tableView reloadData];

}

else return;

}


總結(jié) :這里面一共有以下幾個點?

1 在多選刪除時 在didSelectRowAtIndexPath這個方法中尝胆,根據(jù)cell所在行的索引值將此行的數(shù)據(jù)存到self.deleteArr中 [self.deleteArr addObject:[self.dataArray objectAtIndex:indexPath.row]];

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

}

當(dāng)取消刪除時,要將self.deleteArr中的數(shù)據(jù)移除护桦,不然會造成 (你先選中一行 然后取消選中 但是當(dāng)你點擊刪除按鈕時含衔,這行cell還是會被刪除)

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath? {

}

2 在全選時。將self.dataArr 中的全部數(shù)據(jù)都賦值給self.deleteArr

NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];

[self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionTop];

[self.deleteArr addObjectsFromArray:self.dataArray];

3 執(zhí)行刪除操作時二庵,將self.dataArr中包含self.deleteArr的數(shù)據(jù)移除

self.dataArray removeObjectsInArray:self.deleteArr];

[self.tableView reloadData];


后記:還有一個在cell中添加長按手勢 使tableview進(jìn)入編輯狀態(tài)

在-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 方法中 為cell添加長按手勢

UILongPressGestureRecognizer *longPressed = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressedAct:)];

longPressed.minimumPressDuration = 1;

[cell.contentView addGestureRecognizer:longPressed];


實現(xiàn)長按手勢的響應(yīng)方法:

-(void)longPressedAct:(UILongPressGestureRecognizer *)gesture

{

if(gesture.state == UIGestureRecognizerStateBegan)

{

CGPoint point = [gesture locationInView:self.tableView];

NSIndexPath * indexPath = [self.tableView indexPathForRowAtPoint:point];

if(indexPath == nil) return ;

//add your code here

self.tableView.editing = YES;

}

}

Markdown模式鏈接

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末贪染,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子催享,更是在濱河造成了極大的恐慌抑进,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,590評論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件睡陪,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機兰迫,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,157評論 3 399
  • 文/潘曉璐 我一進(jìn)店門信殊,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人汁果,你說我怎么就攤上這事涡拘。” “怎么了据德?”我有些...
    開封第一講書人閱讀 169,301評論 0 362
  • 文/不壞的土叔 我叫張陵鳄乏,是天一觀的道長。 經(jīng)常有香客問我棘利,道長橱野,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 60,078評論 1 300
  • 正文 為了忘掉前任善玫,我火速辦了婚禮水援,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘茅郎。我一直安慰自己蜗元,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 69,082評論 6 398
  • 文/花漫 我一把揭開白布系冗。 她就那樣靜靜地躺著奕扣,像睡著了一般。 火紅的嫁衣襯著肌膚如雪掌敬。 梳的紋絲不亂的頭發(fā)上惯豆,一...
    開封第一講書人閱讀 52,682評論 1 312
  • 那天,我揣著相機與錄音涝开,去河邊找鬼循帐。 笑死,一個胖子當(dāng)著我的面吹牛舀武,可吹牛的內(nèi)容都是我干的拄养。 我是一名探鬼主播,決...
    沈念sama閱讀 41,155評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼银舱,長吁一口氣:“原來是場噩夢啊……” “哼瘪匿!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起寻馏,我...
    開封第一講書人閱讀 40,098評論 0 277
  • 序言:老撾萬榮一對情侶失蹤棋弥,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后诚欠,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體顽染,經(jīng)...
    沈念sama閱讀 46,638評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡漾岳,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,701評論 3 342
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了粉寞。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片尼荆。...
    茶點故事閱讀 40,852評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖唧垦,靈堂內(nèi)的尸體忽然破棺而出捅儒,到底是詐尸還是另有隱情,我是刑警寧澤振亮,帶...
    沈念sama閱讀 36,520評論 5 351
  • 正文 年R本政府宣布巧还,位于F島的核電站,受9級特大地震影響坊秸,放射性物質(zhì)發(fā)生泄漏麸祷。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 42,181評論 3 335
  • 文/蒙蒙 一妇斤、第九天 我趴在偏房一處隱蔽的房頂上張望摇锋。 院中可真熱鬧,春花似錦站超、人聲如沸荸恕。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,674評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽融求。三九已至,卻和暖如春算撮,著一層夾襖步出監(jiān)牢的瞬間生宛,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,788評論 1 274
  • 我被黑心中介騙來泰國打工肮柜, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留陷舅,地道東北人。 一個月前我還...
    沈念sama閱讀 49,279評論 3 379
  • 正文 我出身青樓审洞,卻偏偏與公主長得像莱睁,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子芒澜,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,851評論 2 361

推薦閱讀更多精彩內(nèi)容