首页 > 代码库 > iOS 修改 TableView 的 delete 按钮

iOS 修改 TableView 的 delete 按钮

在iOS 6 以前重写

-(void)willTransitionToState:(UITableViewCellStateMask)state ,发现iOS7 以前的方法不管用了


iOS7 下

在自定义cell里重写

-(void)willTransitionToState:(UITableViewCellStateMask)state{

    [superwillTransitionToState:state];

    if((state &UITableViewCellStateShowingDeleteConfirmationMask) == UITableViewCellStateShowingDeleteConfirmationMask){

//        [self recurseAndReplaceSubViewIfDeleteConfirmationControl:self.subviews];

        [self performSelector:@selector(recurseAndReplaceSubViewIfDeleteConfirmationControl:)withObject:self.subviewsafterDelay:0];

    }

}


-(void)recurseAndReplaceSubViewIfDeleteConfirmationControl:(NSArray*)subviews{

    NSString *delete_button_name = @"delete";

    for (UIView *subviewin subviews)

    {

        /**

         *  iOS 6 以前

         */

        if ([NSStringFromClass([subviewclass]) isEqualToString:@"UITableViewCellDeleteConfirmationControl"])

        {

            /**

             将添加一个视图来覆盖默认的控制使用的图像有一个透明的背景

             */

            UIView *backgroundCoverDefaultControl = [[UIViewalloc] initWithFrame:CGRectMake(0,0,64, 33)];

            [backgroundCoverDefaultControl setBackgroundColor:[UIColorwhiteColor]];

            [[subview.subviews objectAtIndex:0] addSubview:backgroundCoverDefaultControl];

            UIImage *deleteImage = [UIImageimageNamed:delete_button_name];

            UIImageView *deleteBtn = [[UIImageViewalloc]initWithFrame:CGRectMake(0,0,deleteImage.size.width, deleteImage.size.height)];

            [deleteBtn setImage:[UIImageimageNamed:delete_button_name]];

            [[subview.subviews objectAtIndex:0] addSubview:deleteBtn];

        }

        /**

         *  ios7

         */

        if ([NSStringFromClass([subviewclass]) isEqualToString:@"UITableViewCellDeleteConfirmationButton"])

        {

            UIButton *deleteButton = (UIButton *)subview;

            [deleteButton setImage:[UIImageimageNamed:delete_button_name] forState:UIControlStateNormal];

            [deleteButton setTitle:@""forState:UIControlStateNormal];

            [deleteButton setBackgroundColor:[UIColorclearColor]];

            for(UIView* viewin subview.subviews){

                if([view isKindOfClass:[UILabelclass]]){

                    [view removeFromSuperview];

                }

            }

        }

        if ([NSStringFromClass([subviewclass]) isEqualToString:@"UITableViewCellDeleteConfirmationView"])

        {

            for(UIView* innerSubViewin subview.subviews){

                if(![innerSubView isKindOfClass:[UIButton class]]){

                    [innerSubView removeFromSuperview];

                }

            }

        }

        if([subview.subviewscount]>0){

            [selfrecurseAndReplaceSubViewIfDeleteConfirmationControl:subview.subviews];

        }

        

    }

}



iOS 修改 TableView 的 delete 按钮