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];
    _deleteBtn.enabled = NO;
    [_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)

 //選擇按鈕點擊響應(yīng)事件
- (void)selectedBtn:(UIButton *)button {
    
    _deleteBtn.enabled = YES;
    //支持同時選中多行
    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;

}
} 

代碼鏈接:https://github.com/irembeu/iOS-TableViewSelectCell.git

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末幢竹,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子恩静,更是在濱河造成了極大的恐慌,老刑警劉巖驶乾,帶你破解...
    沈念sama閱讀 218,284評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異级乐,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)风科,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,115評論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來贼穆,“玉大人,你說我怎么就攤上這事故痊。” “怎么了?”我有些...
    開封第一講書人閱讀 164,614評論 0 354
  • 文/不壞的土叔 我叫張陵焰络,是天一觀的道長。 經(jīng)常有香客問我抡柿,道長等恐,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,671評論 1 293
  • 正文 為了忘掉前任课蔬,我火速辦了婚禮,結(jié)果婚禮上二跋,老公的妹妹穿的比我還像新娘。我一直安慰自己扎即,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,699評論 6 392
  • 文/花漫 我一把揭開白布谚鄙。 她就那樣靜靜地躺著,像睡著了一般闷营。 火紅的嫁衣襯著肌膚如雪烤黍。 梳的紋絲不亂的頭發(fā)上傻盟,一...
    開封第一講書人閱讀 51,562評論 1 305
  • 那天,我揣著相機(jī)與錄音娘赴,去河邊找鬼。 笑死诽表,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的关顷。 我是一名探鬼主播,決...
    沈念sama閱讀 40,309評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼议双,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起汞舱,我...
    開封第一講書人閱讀 39,223評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎昂芜,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體泌神,經(jīng)...
    沈念sama閱讀 45,668評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,859評論 3 336
  • 正文 我和宋清朗相戀三年欢际,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片损趋。...
    茶點故事閱讀 39,981評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖浑槽,靈堂內(nèi)的尸體忽然破棺而出蒋失,到底是詐尸還是另有隱情桐玻,我是刑警寧澤,帶...
    沈念sama閱讀 35,705評論 5 347
  • 正文 年R本政府宣布畸冲,位于F島的核電站,受9級特大地震影響邑闲,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜苫耸,卻給世界環(huán)境...
    茶點故事閱讀 41,310評論 3 330
  • 文/蒙蒙 一州邢、第九天 我趴在偏房一處隱蔽的房頂上張望褪子。 院中可真熱鬧,春花似錦嫌褪、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,904評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至摘刑,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間枷恕,已是汗流浹背党晋。 一陣腳步聲響...
    開封第一講書人閱讀 33,023評論 1 270
  • 我被黑心中介騙來泰國打工未玻, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人蛹锰。 一個月前我還...
    沈念sama閱讀 48,146評論 3 370
  • 正文 我出身青樓绰疤,卻偏偏與公主長得像,于是被迫代替她去往敵國和親轻庆。 傳聞我的和親對象是個殘疾皇子癣猾,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,933評論 2 355

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