自定 義cell左滑刪除按鈕

轉載: http://www.reibang.com/p/779f36c21632

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(100, 66, 480, 640) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    [self.view addSubview:self.tableView];
 
    self.dataArray = [NSMutableArray arrayWithObjects:@"a",@"b",@"c",@"d",@"e",@"f", nil];
    
}
- (void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    if (self.editingIndexPath) {
        [self configSwipeButtons];
    }
    
}
- (void)configSwipeButtons
{
    // 獲取選項按鈕的reference
    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"11.0"))
    {
        // iOS 11層級 (Xcode 9編譯): UITableView -> UISwipeActionPullView
        for (UIView *subview in self.tableView.subviews)
        {
            if ([subview isKindOfClass:NSClassFromString(@"UISwipeActionPullView")] && [subview.subviews count] >= 2)
            {
                // 和iOS 10的按鈕順序相反
                UIButton *deleteButton = subview.subviews[1];
                UIButton *readButton = subview.subviews[0];
                [self configDeleteButton:deleteButton];
                [self configReadButton:readButton];
            }
        }
    }
    else
    {
        // iOS 8-10層級: UITableView -> UITableViewCell -> UITableViewCellDeleteConfirmationView
        UITableViewCell *tableCell = [self.tableView cellForRowAtIndexPath:self.editingIndexPath];
        for (UIView *subview in tableCell.subviews)
        {
            if ([subview isKindOfClass:NSClassFromString(@"UITableViewCellDeleteConfirmationView")] && [subview.subviews count] >= 2)
            {
                UIButton *deleteButton = subview.subviews[0];
                UIButton *readButton = subview.subviews[1];
                [self configDeleteButton:deleteButton];
                [self configReadButton:readButton];
//                [subview setBackgroundColor:[UIColor redColor]];
            }
        }
    }
 
}

- (void)configDeleteButton:(UIButton*)deleteButton
{
    if (deleteButton)
    {
        [deleteButton.titleLabel setFont:[UIFont fontWithName:@"SFUIText-Regular" size:12.0]];
        [deleteButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        [deleteButton setImage:[UIImage imageNamed:@"Delete_icon_.png"] forState:UIControlStateNormal];
        [deleteButton setBackgroundColor:[UIColor whiteColor]];
        // 調整按鈕上圖片和文字的相對位置(該方法的實現在下面)
        [self centerImageAndTextOnButton:deleteButton];
    }
}

- (void)configReadButton:(UIButton*)readButton
{
    if (readButton)
    {
        [readButton.titleLabel setFont:[UIFont fontWithName:@"SFUIText-Regular" size:12.0]];
        [readButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        // 根據當前狀態(tài)選擇不同圖片
        UIImage *readButtonImage = [UIImage imageNamed: @"Mark_as_read_icon_.png"];
        [readButton setImage:readButtonImage forState:UIControlStateNormal];
        
        [readButton setBackgroundColor:[UIColor whiteColor]];
        // 調整按鈕上圖片和文字的相對位置(該方法的實現在下面)
        [self centerImageAndTextOnButton:readButton];
    }
}


- (void)centerImageAndTextOnButton:(UIButton*)button
{
    // this is to center the image and text on button.
    // the space between the image and text
    CGFloat spacing = 35.0;
    
    // lower the text and push it left so it appears centered below the image
    CGSize imageSize = button.imageView.image.size;
    button.titleEdgeInsets = UIEdgeInsetsMake(0.0, - imageSize.width, - (imageSize.height + spacing), 0.0);
    
    // raise the image and push it right so it appears centered above the text
    CGSize titleSize = [button.titleLabel.text sizeWithAttributes:@{NSFontAttributeName: button.titleLabel.font}];
    button.imageEdgeInsets = UIEdgeInsetsMake(-(titleSize.height + spacing), 0.0, 0.0, - titleSize.width);
    
    // increase the content height to avoid clipping
    CGFloat edgeOffset = (titleSize.height - imageSize.height) / 2.0;
    button.contentEdgeInsets = UIEdgeInsetsMake(edgeOffset, 0.0, edgeOffset, 0.0);
    
    // move whole button down, apple placed the button too high in iOS 10
    if (SYSTEM_VERSION_LESS_THAN(@"11.0"))
    {
        CGRect btnFrame = button.frame;
        btnFrame.origin.y = 18;
        button.frame = btnFrame;
    }
}

- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.editingIndexPath = indexPath;
    [self.view setNeedsLayout];   // 觸發(fā)-(void)viewDidLayoutSubviews
}

- (void)tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.editingIndexPath = nil;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 120;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellID = @"tableview.cell.id";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellID];
    }
    cell.textLabel.text = [NSString stringWithFormat:@"第%d cell",indexPath.row];
    
    return cell;
    
}
- (NSArray*)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    // delete action
    UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:NSLocalizedString(@"Delete", @"") handler:^(UITableViewRowAction *action, NSIndexPath *indexPath)
                                          {
                                              [tableView setEditing:NO animated:YES];  // 這句很重要夭拌,退出編輯模式,隱藏左滑菜單
                                              [self removeNotificationAction:indexPath];
                                          }];
    
    // read action
    // 根據cell當前的狀態(tài)改變選項文字
    NSString *readTitle = @"Read";
    // 創(chuàng)建action
    UITableViewRowAction *readAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:readTitle handler:^(UITableViewRowAction *action, NSIndexPath *indexPath)
                                        {
                                            [tableView setEditing:NO animated:YES];  // 這句很重要榴芳,退出編輯模式歧蒋,隱藏左滑菜單
                                        }];
    
    return @[deleteAction, readAction];
}
- (void)removeNotificationAction:(NSIndexPath *)indexpath
{
    [self.dataArray removeObjectAtIndex:indexpath.row];
    [self.tableView reloadData];
}



?著作權歸作者所有,轉載或內容合作請聯系作者
  • 序言:七十年代末删壮,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子跷坝,更是在濱河造成了極大的恐慌倦蚪,老刑警劉巖侣诵,帶你破解...
    沈念sama閱讀 219,490評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件痢法,死亡現場離奇詭異,居然都是意外死亡杜顺,警方通過查閱死者的電腦和手機财搁,發(fā)現死者居然都...
    沈念sama閱讀 93,581評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來躬络,“玉大人尖奔,你說我怎么就攤上這事∏畹保” “怎么了提茁?”我有些...
    開封第一講書人閱讀 165,830評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長馁菜。 經常有香客問我茴扁,道長,這世上最難降的妖魔是什么汪疮? 我笑而不...
    開封第一講書人閱讀 58,957評論 1 295
  • 正文 為了忘掉前任峭火,我火速辦了婚禮,結果婚禮上智嚷,老公的妹妹穿的比我還像新娘卖丸。我一直安慰自己,他們只是感情好盏道,可當我...
    茶點故事閱讀 67,974評論 6 393
  • 文/花漫 我一把揭開白布坯苹。 她就那樣靜靜地躺著,像睡著了一般摇天。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上恐仑,一...
    開封第一講書人閱讀 51,754評論 1 307
  • 那天泉坐,我揣著相機與錄音,去河邊找鬼裳仆。 笑死腕让,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播纯丸,決...
    沈念sama閱讀 40,464評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼偏形,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了觉鼻?” 一聲冷哼從身側響起俊扭,我...
    開封第一講書人閱讀 39,357評論 0 276
  • 序言:老撾萬榮一對情侶失蹤扭勉,失蹤者是張志新(化名)和其女友劉穎喷屋,沒想到半個月后虐呻,有當地人在樹林里發(fā)現了一具尸體啄巧,經...
    沈念sama閱讀 45,847評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡赂毯,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,995評論 3 338
  • 正文 我和宋清朗相戀三年尚胞,在試婚紗的時候發(fā)現自己被綠了缆蝉。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片晓铆。...
    茶點故事閱讀 40,137評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡贮匕,死狀恐怖姐仅,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情刻盐,我是刑警寧澤掏膏,帶...
    沈念sama閱讀 35,819評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站隙疚,受9級特大地震影響壤追,放射性物質發(fā)生泄漏。R本人自食惡果不足惜供屉,卻給世界環(huán)境...
    茶點故事閱讀 41,482評論 3 331
  • 文/蒙蒙 一行冰、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧伶丐,春花似錦悼做、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,023評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至录别,卻和暖如春朽色,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背组题。 一陣腳步聲響...
    開封第一講書人閱讀 33,149評論 1 272
  • 我被黑心中介騙來泰國打工葫男, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人崔列。 一個月前我還...
    沈念sama閱讀 48,409評論 3 373
  • 正文 我出身青樓梢褐,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子盈咳,可洞房花燭夜當晚...
    茶點故事閱讀 45,086評論 2 355

推薦閱讀更多精彩內容