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

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

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

clipboard.png

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

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:@"未选择"];
                    }
                }
            }
        }
    }
}
相关文章
相关标签/搜索