自定义UITableViewCell的多选样式

简单来说就是隐藏系统的UITableViewCellEditControl
添加一个自己的view.

@property(nonatomic, strong) UIImageView *customEdit;

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {

    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {

        UIImage *image = [UIImage imageNamed:@"image"];
        UIImage *highlightedImage = [UIImage imageNamed:@"highlightedImage"];

        _customEdit = [[UIImageView alloc] initWithImage:image highlightedImage:highlightedImage];
        [self.contentView addSubview:_customEdit];
        [_customEdit mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(_nameLabel.mas_left).offset(-9);
            make.centerY.equalTo(_nameLabel.mas_centerY);
        }];
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    self.customEdit.highlighted = selected;
}

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
    [super setHighlighted:highlighted animated:animated];
    self.customEdit.highlighted = highlighted;
}

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

    if (state == UITableViewCellStateShowingEditControlMask) {
        for (UIView *subview in self.subviews) {
            if ([subview isMemberOfClass:NSClassFromString(@"UITableViewCellEditControl")]) {
                subview.hidden = YES;
            }
        }
    }
}

你可能感兴趣的:(自定义UITableViewCell的多选样式)