您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

iOS:替換 UITableView 左滑刪除按鈕的樣式

編輯:iOS 開發綜合

在 iOS 6 以前,做法是覆寫 -(void)willTransitionToState: 去攔下刪除確認控制項。但升到 iOS 7 之後,舊寫法就完全失效了,必須改用新的類別名稱來處理。

iOS 7 下的實作

在自訂的 cell 類別中覆寫狀態轉換方法:

- (void)willTransitionToState:(UITableViewCellStateMask)state {
    [super willTransitionToState:state];

    if ((state & UITableViewCellStateShowingDeleteConfirmationMask) ==
         UITableViewCellStateShowingDeleteConfirmationMask) {
        [self performSelector:@selector(recurseAndReplaceSubViewIfDeleteConfirmationControl:)
                   withObject:self.subviews
                   afterDelay:0];
    }
}

這裡刻意用 performSelector:afterDelay:0 延後一輪執行,因為在狀態轉換的當下,系統的刪除控制項尚未完全建立完成,直接處理會抓不到目標視圖。

- (void)recurseAndReplaceSubViewIfDeleteConfirmationControl:(NSArray *)subviews {
    NSString *deleteButtonName = @"delete";

    for (UIView *subview in subviews) {
        // iOS 6 以前:UITableViewCellDeleteConfirmationControl
        if ([NSStringFromClass([subview class]) isEqualToString:@"UITableViewCellDeleteConfirmationControl"]) {
            // 用一個不透明的 UIView 蓋掉預設控制項,再疊上自訂圖片
            UIView *cover = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 64, 33)];
            [cover setBackgroundColor:[UIColor whiteColor]];
            [[subview.subviews objectAtIndex:0] addSubview:cover];

            UIImage *deleteImage = [UIImage imageNamed:deleteButtonName];
            UIImageView *deleteBtn = [[UIImageView alloc] initWithFrame:
                                      CGRectMake(0, 0, deleteImage.size.width, deleteImage.size.height)];
            [deleteBtn setImage:deleteImage];
            [[subview.subviews objectAtIndex:0] addSubview:deleteBtn];
        }

        // iOS 7:UITableViewCellDeleteConfirmationButton
        if ([NSStringFromClass([subview class]) isEqualToString:@"UITableViewCellDeleteConfirmationButton"]) {
            UIButton *deleteButton = (UIButton *)subview;
            [deleteButton setImage:[UIImage imageNamed:deleteButtonName]
                          forState:UIControlStateNormal];
            [deleteButton setTitle:@"" forState:UIControlStateNormal];
            [deleteButton setBackgroundColor:[UIColor clearColor]];

            // 移除系統預設的文字 Label
            for (UIView *view in subview.subviews) {
                if ([view isKindOfClass:[UILabel class]]) {
                    [view removeFromSuperview];
                }
            }
        }

        // iOS 7:UITableViewCellDeleteConfirmationView,清掉非按鈕的殘留子視圖
        if ([NSStringFromClass([subview class]) isEqualToString:@"UITableViewCellDeleteConfirmationView"]) {
            for (UIView *inner in subview.subviews) {
                if (![inner isKindOfClass:[UIButton class]]) {
                    [inner removeFromSuperview];
                }
            }
        }

        // 遞迴往下處理巢狀視圖
        if ([subview.subviews count] > 0) {
            [self recurseAndReplaceSubViewIfDeleteConfirmationControl:subview.subviews];
        }
    }
}

整體思路是:攔下狀態轉換時機,遞迴遍歷視圖樹,依類別名稱分別處理 iOS 6 與 iOS 7 的刪除控制項,把系統預設的文字與背景清掉,換上自己的圖片。這種做法依賴私有類別名稱,系統升版後仍有失效風險,若只是要換圖示,優先考慮 UITableViewRowAction 或自訂側滑容器會更穩妥。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved