IOS 兼容ios11以前和ios11以后的TableView自定義刪除按鈕

//cell
/*自定義設(shè)置iOS8-10系統(tǒng)下的左滑刪除按鈕大小/


- (void)layoutSubviews {
    [super layoutSubviews];
    
    for (UIView * subView in self.subviews) {
        if ([subView isKindOfClass:NSClassFromString(@"UITableViewCellDeleteConfirmationView")]) {
            subView.backgroundColor = [UIColor clearColor];//去掉默認(rèn)紅色背景
            
            UIButton * stopButton = subView.subviews[0];
            [stopButton setImage:[UIImage imageNamed:@"stop_btn"] forState:UIControlStateNormal];
            [stopButton setTitle:@"" forState:UIControlStateNormal];
            
            UIButton * editButton = subView.subviews[1];
            [editButton setTitle:@"" forState:UIControlStateNormal];
            
            
            [editButton setImage:[UIImage imageNamed:@"edit_btn"] forState:UIControlStateNormal];
        }
    }
}

-----------------TableViewDelegate,UITableViewDataSource-----------------
/*自定義設(shè)置iOS11系統(tǒng)下的左滑刪除按鈕大小/



#pragma mark - 設(shè)置刪除代理方法
/**使用系統(tǒng)默認(rèn)的刪除按鈕
 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
 if (editingStyle == UITableViewCellEditingStyleDelete){
 
 }
 }
 //自定義系統(tǒng)默認(rèn)的刪除按鈕文字
 - (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
 return @"自定義按鈕";
 }
 */

//自定義多個左滑菜單選項
- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"停用" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        [tableView setEditing:NO animated:YES];//退出編輯模式狂巢,隱藏左滑菜單
    }];
    UITableViewRowAction *shareAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"啟用" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        [tableView setEditing:NO animated:YES];//退出編輯模式路幸,隱藏左滑菜單
    }];
    
    deleteAction.backgroundColor = [UIColor LineColor];
    shareAction.backgroundColor = [UIColor LineColor];
    return @[deleteAction,shareAction];
}
/**自定義設(shè)置iOS11系統(tǒng)下的左滑刪除按鈕大小*/
//開始編輯左滑刪除
- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath {
    if (@available(iOS 11.0, *)) {
        for (UIView * subView in self.tableView.subviews) {
            if ([subView isKindOfClass:NSClassFromString(@"UISwipeActionPullView")]) {
                subView.backgroundColor = [UIColor clearColor];//如果自定義只有一個按鈕就要去掉按鈕默認(rèn)紅色背景
                
                UIButton * stopButton = subView.subviews[0];
                [stopButton setImage:[UIImage imageNamed:@"stop_btn"] forState:UIControlStateNormal];
                [stopButton setTitle:@"" forState:UIControlStateNormal];
                
                UIButton * editButton = subView.subviews[1];
                [editButton setImage:[UIImage imageNamed:@"edit_btn"] forState:UIControlStateNormal];
                [editButton setTitle:@"" forState:UIControlStateNormal];

               
                [stopButton addTarget:self action:@selector(btnEditClick:) forControlEvents:UIControlEventTouchUpInside];
                [editButton addTarget:self action:@selector(btnStopClick:) forControlEvents:UIControlEventTouchUpInside];
            }
        }
    }
}

//結(jié)束編輯左滑刪除
- (void)tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath {
    
}

//判斷是否顯示左滑刪除
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}


?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末钉凌,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子注暗,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,734評論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件夯膀,死亡現(xiàn)場離奇詭異,居然都是意外死亡回俐,警方通過查閱死者的電腦和手機(jī)逛腿,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,931評論 3 394
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來仅颇,“玉大人单默,你說我怎么就攤上這事⊥撸” “怎么了搁廓?”我有些...
    開封第一講書人閱讀 164,133評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長耕皮。 經(jīng)常有香客問我境蜕,道長,這世上最難降的妖魔是什么凌停? 我笑而不...
    開封第一講書人閱讀 58,532評論 1 293
  • 正文 為了忘掉前任粱年,我火速辦了婚禮,結(jié)果婚禮上罚拟,老公的妹妹穿的比我還像新娘台诗。我一直安慰自己完箩,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,585評論 6 392
  • 文/花漫 我一把揭開白布拉队。 她就那樣靜靜地躺著弊知,像睡著了一般。 火紅的嫁衣襯著肌膚如雪粱快。 梳的紋絲不亂的頭發(fā)上秩彤,一...
    開封第一講書人閱讀 51,462評論 1 302
  • 那天,我揣著相機(jī)與錄音皆尔,去河邊找鬼呐舔。 笑死,一個胖子當(dāng)著我的面吹牛慷蠕,可吹牛的內(nèi)容都是我干的珊拼。 我是一名探鬼主播,決...
    沈念sama閱讀 40,262評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼流炕,長吁一口氣:“原來是場噩夢啊……” “哼澎现!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起每辟,我...
    開封第一講書人閱讀 39,153評論 0 276
  • 序言:老撾萬榮一對情侶失蹤剑辫,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后渠欺,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體妹蔽,經(jīng)...
    沈念sama閱讀 45,587評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,792評論 3 336
  • 正文 我和宋清朗相戀三年挠将,在試婚紗的時候發(fā)現(xiàn)自己被綠了胳岂。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,919評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡舔稀,死狀恐怖乳丰,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情内贮,我是刑警寧澤产园,帶...
    沈念sama閱讀 35,635評論 5 345
  • 正文 年R本政府宣布,位于F島的核電站夜郁,受9級特大地震影響什燕,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜竞端,卻給世界環(huán)境...
    茶點故事閱讀 41,237評論 3 329
  • 文/蒙蒙 一秋冰、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧婶熬,春花似錦剑勾、人聲如沸埃撵。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,855評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽暂刘。三九已至,卻和暖如春捂刺,著一層夾襖步出監(jiān)牢的瞬間谣拣,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,983評論 1 269
  • 我被黑心中介騙來泰國打工族展, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留森缠,地道東北人。 一個月前我還...
    沈念sama閱讀 48,048評論 3 370
  • 正文 我出身青樓仪缸,卻偏偏與公主長得像贵涵,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子恰画,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,864評論 2 354

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