UITableView自定義左劃刪除遇到的問題

xcode9.1   ios8.1系統   UITableView的層級結構:      Xcode9.1    ios11 系統   UITableView的層級結構:ios

 

ios8.0之後自定義左劃刪除功能有個新的方法:(在左邊添加兩個按鈕)xcode

- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewRowAction *action0 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:nil handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        PLog(@"點擊了發標");
        CKLWaitChectModel *model = self.allData[indexPath.row];
        [self putBorrow:model.ID];
        // 收回左滑出現的按鈕(退出編輯模式)
        tableView.editing = NO;
    }];

    UITableViewRowAction *action1 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:nil handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        PLog(@"點擊了刪除");
        CKLWaitChectModel *model = self.allData[indexPath.row];
        [self deleteOne:model.ID];
        // 收回左滑出現的按鈕(退出編輯模式)
        tableView.editing = NO;

    }];

    return @[action1, action0];

}

實現了上面這個方法之後就不會再執行下面這個方法,由於都是處理左邊劃出按鈕點擊事件的spa

注:ios11 如下的系統,下面這個方法(↓)必須實現,可是裏面能夠不作任何操做,若是不實現就不會有滑動功能code

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    PLog(@"這個方法是爲了兼容ios11如下的系統,這裏面能夠不實現任何功能");
}

設置編輯模式:(注:ios11 如下系統左劃風格不支持UITableViewCellEditingStyleInsert | UITableViewCellEditingStyleDelete這種格式,若是真想用這種左邊選中對勾的風格就得判斷下cell是否是左劃,而後根據狀態給出對應的enumorm

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath;

/*
    UITableViewCellEditingStyleNone, 
    UITableViewCellEditingStyleDelete, 進入編輯狀態後左邊「減號」 ,右邊Delete
    UITableViewCellEditingStyleInsert  進入編輯狀態後左邊「加號」
*/
UITableViewCellEditingStyleInsert | UITableViewCellEditingStyleDelete 進入編輯狀態後左邊就是可選的「對勾」

只要實現上面兩個方法後就具有左劃功能了,只是右邊的按鈕效果不是咱們想要的而已,下面咱們就來自定義右邊的按鈕事件

文章的開頭咱們已經瞭解了tableview的層級結構,ios11之後側滑視圖是直接添加在tableview上的,ios11之前是添加在cell上的,咱們要作出相應的適配(我這裏是統一在tableview中捕獲修改了,還能夠分開來作)ip

我是自定義了tableview,因此寫在了layoutsubviews方法裏面,沒自定義的話就寫在viewDidLayoutSubviews裏面:rem

- (void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    NSLog(@"%s", __func__);
        
        //    NSLog(@"tableview的子視圖:::=++==+= = ==%@", self.subviews);
        // 獲取選項按鈕的reference
        if (@available(iOS 11, *)){
            // iOS 11層級 (Xcode 9編譯): UITableView -> UISwipeActionPullView
            for (UIView *subview in self.table.subviews)
            {
                if ([subview isKindOfClass:NSClassFromString(@"UISwipeActionPullView")] && [subview.subviews count] >= 2)
                {
                    subview.backgroundColor = [UIColor whiteColor];
                    CGFloat btnHeight = subview.frame.size.height;
                    CGFloat btnW = 60;
                    UIButton *deleteButton = subview.subviews[1];
                    UIButton *readButton = subview.subviews[0];
                    
                    CGRect subFrame = subview.frame;
                    subFrame = CGRectMake(DEVICE_AVALIABLE_WIDTH - 170, subFrame.origin.y, 170, subFrame.size.width);
                    subview.frame = subFrame;
                    
                    deleteButton.frame = CGRectMake(70, (btnHeight - btnW) * 0.5, btnW, btnW);
                    deleteButton.layer.cornerRadius = 3;
                    deleteButton.layer.masksToBounds = YES;
                    [deleteButton.titleLabel removeFromSuperview];
                    
                    UILabel *deleteLB = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, btnW, btnW)];
                    deleteLB.textAlignment = NSTextAlignmentCenter;
                    deleteLB.text = @"移除";
                    deleteLB.font = [UIFont fontWithName:@"PingFangSC-Light" size:14];
                    deleteLB.textColor = [UIColor whiteColor];
                    [deleteButton addSubview:deleteLB];
                    deleteButton.backgroundColor = [UIColor redColor];
                    
                    readButton.frame = CGRectMake(0, (btnHeight - btnW) * 0.5, btnW, btnW);
                    readButton.titleLabel.textAlignment = NSTextAlignmentCenter;
                    readButton.layer.cornerRadius = 3;
                    readButton.layer.masksToBounds = YES;
                    [readButton.titleLabel removeFromSuperview];
                    UILabel *readButtonLB = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, btnW, btnW)];
                    readButtonLB.textAlignment = NSTextAlignmentCenter;
                    readButtonLB.text = @"發標";
                    readButtonLB.font = [UIFont fontWithName:@"PingFangSC-Light" size:14];
                    readButtonLB.textColor = [UIColor whiteColor];
                    [readButton addSubview:readButtonLB];
                    readButton.backgroundColor = [UIColor blueColor];
                }
            }
            
        }
        else{
            // iOS 8-10層級: UITableView -> UITableViewCell -> UITableViewCellDeleteConfirmationView
            UITableViewCell *cell = [self.table cellForRowAtIndexPath:self.editingIndexPath];
            for (UIView *subview in cell.subviews){
                if ([subview isKindOfClass:NSClassFromString(@"UITableViewCellDeleteConfirmationView")] && [subview.subviews count] >= 2){
                    
                    subview.backgroundColor = [UIColor blueColor];
                    CGFloat btnHeight = subview.frame.size.height;
                    CGFloat btnW = 60;
                    UIButton *deleteButton = subview.subviews[1];
                    UIButton *readButton = subview.subviews[0];
                    
                    deleteButton.frame = CGRectMake(70, (btnHeight - btnW) * 0.5, btnW, btnW);
                    deleteButton.layer.cornerRadius = 3;
                    deleteButton.layer.masksToBounds = YES;
                    [deleteButton.titleLabel removeFromSuperview];
                    UILabel *deleteLB = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, btnW, btnW)];
                    deleteLB.textAlignment = NSTextAlignmentCenter;
                    deleteLB.text = @"移除";
                    deleteLB.font = [UIFont fontWithName:@"PingFangSC-Light" size:14];
                    deleteLB.textColor = [UIColor whiteColor];
                    [deleteButton addSubview:deleteLB];
                    deleteButton.backgroundColor = [UIColor redColor];
                    
                    readButton.frame = CGRectMake(0, (btnHeight - btnW) * 0.5, btnW, btnW);
                    readButton.titleLabel.textAlignment = NSTextAlignmentCenter;
                    readButton.layer.cornerRadius = 3;
                    readButton.layer.masksToBounds = YES;
                    [deleteButton.titleLabel removeFromSuperview];
                    UILabel *readButtonLB = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, btnW, btnW)];
                    readButtonLB.textAlignment = NSTextAlignmentCenter;
                    readButtonLB.text = @"發標";
                    readButtonLB.font = [UIFont fontWithName:@"PingFangSC-Light" size:14];
                    readButtonLB.textColor = [UIColor whiteColor];
                    [readButton addSubview:readButtonLB];
                    readButton.backgroundColor = [UIColor yellowColor];
                    
                    
                }
            }
            
        }

}

注:IOS13cell的層級結構又有變化it

if (@available(iOS 13.0, *)) {
                if ([subView isKindOfClass:NSClassFromString(@"_UITableViewCellSwipeContainerView")]) {
                    for (UIView *sb in subView.subviews) {
                        if ([sb isKindOfClass:NSClassFromString(@"UISwipeActionPullView")]) {
相關文章
相關標籤/搜索