美文网首页iOS UI
自定义UITableViewCell的多选样式

自定义UITableViewCell的多选样式

作者: 修正 | 来源:发表于2018-09-21 10:55 被阅读0次

    简单来说就是隐藏系统的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的多选样式

        本文链接:https://www.haomeiwen.com/subject/tdranftx.html