tableView-刪除毁靶、插入、多選刪除

  • 左滑刪除
//聲明
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;//數(shù)據(jù)源

//懶加載
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}

- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

//添加tableView
- (void)createUI
{
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.and.trailing.equalTo(@0);
        make.top.equalTo(self.mas_topLayoutGuide);
        make.bottom.equalTo(self.mas_bottomLayoutGuide);
    }];
}

//數(shù)據(jù)
- (void)loadData
{
    for (int i = 0; i < 20; i ++) {
        [self.dataArray addObject:[NSString stringWithFormat:@"%d", i]];
    }
}

//數(shù)據(jù)源代理 -展示數(shù)據(jù)
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *iden = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:iden];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:iden];
    }
    
    cell.textLabel.text = self.dataArray[indexPath.row];
    
    return cell;
}

//刪除代理
//是否可編輯
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
        [self.dataArray removeObjectAtIndex:indexPath.row];//刪除數(shù)據(jù)源中相應數(shù)據(jù)
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}

//修改刪除操作title -默認 "刪除"(Delete) -跟隨系統(tǒng)語言
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"左滑刪除";
}

左滑刪除.gif
  • 編輯狀態(tài)下刪除

與左滑返回區(qū)別: 設置編輯狀態(tài)為YES

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

//編輯風格為刪除
if (self.editingStyle == CCTableViewCellEditingStyleDelete) {
            return UITableViewCellEditingStyleDelete;
        }

點擊編輯逊移,出現(xiàn)減號预吆,再點擊減號 出現(xiàn)左滑返回按鈕 點擊左滑返回按鈕 刪除數(shù)據(jù)

編輯刪除.gif
  • 編輯狀態(tài)下插入
//編輯風格為插入
if (self.editingStyle == CCTableViewCellEditingStyleInsert) {
            return UITableViewCellEditingStyleInsert;
        }

//插入操作
[self.dataArray insertObject:@"cc" atIndex:indexPath.row];//插入數(shù)據(jù)
        [tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
編輯插入.gif
  • 編輯風格為刪除和插入共存
if (self.editingStyle == (CCTableViewCellEditingStyleDelete | CCTableViewCellEditingStyleInsert)) {
            return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
        }

//聲明
@property (nonatomic, strong) NSMutableDictionary *deleteDic;//記錄多選刪除數(shù)據(jù)
@property (nonatomic, strong) UIBarButtonItem *deleteBtn;

//顯示刪除按鈕
if (self.editingStyle == (CCTableViewCellEditingStyleInsert | CCTableViewCellEditingStyleDelete)) {
        
        [self.deleteDic removeAllObjects];
        
        self.deleteBtn = [[UIBarButtonItem alloc] initWithTitle:@"刪除" style:UIBarButtonItemStylePlain target:self action:@selector(deleteBtnAction)];
        NSMutableArray *Items = [[NSMutableArray alloc]initWithObjects:self.deleteBtn, nil];
        
        if (self.tableView.editing) {
            [self.navigationController setToolbarHidden:NO animated:YES];
        }else {
            [self.navigationController setToolbarHidden:YES animated:YES];
        }
        
        [self setToolbarItems:Items];
        self.deleteBtn.enabled = NO;
    }

//點擊cell選擇刪除行
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //indexPath為key 對應數(shù)據(jù)為值
    [self.deleteDic setObject:_dataArray[indexPath.row] forKey:indexPath];
    if (self.deleteDic.count == 0) {
        self.deleteBtn.enabled = NO;
    } else {
        self.deleteBtn.enabled = YES;
    }
}

//取消行
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.deleteDic removeObjectForKey:indexPath];
    if (self.deleteDic.count == 0) {
        self.deleteBtn.enabled = NO;
    } else {
        self.deleteBtn.enabled = YES;
    }
}

//刪除操作
//多選刪除操作
    [_dataArray removeObjectsInArray:[self.deleteDic allValues]];
    [self.tableView deleteRowsAtIndexPaths:[self.deleteDic allKeys] withRowAnimation:UITableViewRowAnimationAutomatic];
    [self.deleteDic removeAllObjects];
    [self.tableView setEditing:!self.tableView.editing animated:YES];
    [self.navigationController setToolbarHidden:YES animated:YES];
多選刪除.gif

左滑刪除很多應用中都會此功能淤齐,而其他的系統(tǒng)自帶功能或許并不一定符合實際需求矛缨;關于多選刪除,過幾天會分享一個小例子

代碼

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末萄焦,一起剝皮案震驚了整個濱河市扇商,隨后出現(xiàn)的幾起案子凤瘦,更是在濱河造成了極大的恐慌,老刑警劉巖钳吟,帶你破解...
    沈念sama閱讀 222,627評論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件廷粒,死亡現(xiàn)場離奇詭異,居然都是意外死亡红且,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,180評論 3 399
  • 文/潘曉璐 我一進店門涤姊,熙熙樓的掌柜王于貴愁眉苦臉地迎上來暇番,“玉大人,你說我怎么就攤上這事思喊”诔辏” “怎么了?”我有些...
    開封第一講書人閱讀 169,346評論 0 362
  • 文/不壞的土叔 我叫張陵恨课,是天一觀的道長舆乔。 經(jīng)常有香客問我,道長剂公,這世上最難降的妖魔是什么希俩? 我笑而不...
    開封第一講書人閱讀 60,097評論 1 300
  • 正文 為了忘掉前任,我火速辦了婚禮纲辽,結(jié)果婚禮上颜武,老公的妹妹穿的比我還像新娘。我一直安慰自己拖吼,他們只是感情好鳞上,可當我...
    茶點故事閱讀 69,100評論 6 398
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著吊档,像睡著了一般篙议。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上怠硼,一...
    開封第一講書人閱讀 52,696評論 1 312
  • 那天鬼贱,我揣著相機與錄音趾断,去河邊找鬼。 笑死吩愧,一個胖子當著我的面吹牛芋酌,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播雁佳,決...
    沈念sama閱讀 41,165評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼脐帝,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了糖权?” 一聲冷哼從身側(cè)響起堵腹,我...
    開封第一講書人閱讀 40,108評論 0 277
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎星澳,沒想到半個月后疚顷,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,646評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡禁偎,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,709評論 3 342
  • 正文 我和宋清朗相戀三年腿堤,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片如暖。...
    茶點故事閱讀 40,861評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡笆檀,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出盒至,到底是詐尸還是另有隱情酗洒,我是刑警寧澤,帶...
    沈念sama閱讀 36,527評論 5 351
  • 正文 年R本政府宣布枷遂,位于F島的核電站樱衷,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏酒唉。R本人自食惡果不足惜矩桂,卻給世界環(huán)境...
    茶點故事閱讀 42,196評論 3 336
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望黔州。 院中可真熱鬧耍鬓,春花似錦、人聲如沸流妻。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,698評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽绅这。三九已至涣达,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背度苔。 一陣腳步聲響...
    開封第一講書人閱讀 33,804評論 1 274
  • 我被黑心中介騙來泰國打工匆篓, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人寇窑。 一個月前我還...
    沈念sama閱讀 49,287評論 3 379
  • 正文 我出身青樓鸦概,卻偏偏與公主長得像,于是被迫代替她去往敵國和親甩骏。 傳聞我的和親對象是個殘疾皇子窗市,可洞房花燭夜當晚...
    茶點故事閱讀 45,860評論 2 361

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