TableviewCell在编辑模式下的多选按钮自定义

在编辑模式下,如果我们启用多选模式,系统则会为我们配上原生的选择按钮。但这往往是不符合UI要求的,如此我们便需要对按钮进行自定义。

不过很可惜,这个按钮属性不是暴露在外的,那我们需要用比较暴力的方法——将它循环出来。

《TableviewCell在编辑模式下的多选按钮自定义》

首先看cell的subview,我们可以发现有这样一个类“UITableViewCellEditControl”。没错,这就是我们要找的东西,它就是cell在进入编辑模式向右缩进后所暴露出来的界面,而它包含着的那个UIImageView自然就是系统原生的多选按钮之所在了。我们只要将它替换即可。

here is the code,将其copy入cell文件中即可:


- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{   
//重写此方法,作用为当进入编辑模式时候运行customMultipleChioce方法
    [super setEditing:editing animated:animated];
    if (editing) {
        [self customMultipleChioce];
    }
}

-(void)layoutSubviews
{   
//重写此方法,作用为当cell重新绘制的时候运行customMultipleChioce方法
    [self customMultipleChioce];
    [super layoutSubviews];
}

-(void)customMultipleChioce{
    for (UIControl *control in self.subviews){  
    //循环cell的subview
        if ([control isMemberOfClass:NSClassFromString(@"UITableViewCellEditControl")]){   
        //找出UITableViewCellEditControl
            for (UIView *view in control.subviews)
            {
                if ([view isKindOfClass: [UIImageView class]]) {   
                //在UITableViewCellEditControl中找到imageView
                    UIImageView *img=(UIImageView *)view;
                    //这样便可以更改按钮的坐标
                    img.frame = CGRectMake(20, img.frame.origin.y, img.frame.size.width, img.frame.size.height);
                    //更改按钮图片
                    if (self.selected) {
                        img.image=[UIImage imageNamed:@"已选择"];
                    }else
                    {
                        img.image=[UIImage imageNamed:@"未选择"];
                    }
                }
            }
        }
    }
}
    原文作者:枢机主教
    原文地址: https://segmentfault.com/a/1190000009121655
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞