mas_updateConstraints的坑

实现cell点击展开与关闭

特么的这个方法并不会覆盖约束。。。。特么的

- (NSArray *)mas_updateConstraints:(void(^)(MASConstraintMaker *make))block {
}

坑代码如下:

####### 目的实现cell评论的展开和关闭:

#import "KapReplyView.h"
// 根据回复的数目自适应ReplyView高度:
- (void)setModelArray:(NSArray *)modelArray{
    _modelArray = modelArray;
    [self cleanSubItemView];
    for (id model in modelArray) {
        KapReplyItemView *view = [self createdItemByModel:model];
        [self addSubview:view];
        [self.itemViewsArray addObject:view];
        float height = PCH_BitMap_BY_SIZE(200);
        if(!_lastView){
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self);
                make.left.right.equalTo(self);
                make.height.mas_equalTo(height);
            }];
        }else{
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(_lastView.mas_bottom);
                make.left.right.equalTo(self);
                make.height.mas_equalTo(height);
            }];
        }
        _lastView = view;
    }
  // 注意下面的代码。。。。。。(把下面移到外层,mas_updateConstraints 改成 mas_remakeConstraints搞定了 擦。。。。)
    if (!_lastView) {
        [self mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.lineView);
        }];
        return;
    }
    [self mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(_lastView);
    }];
}
#import "KapCommentCell.h"
- (void)changeCommentReplyArray:(NSArray *)array{
    if (self.showButton.selected) {// 展开状态
        self.replyView.modelArray = array;
    }else{
        self.replyView.modelArray = @[];
    }
}

上面的代码修正为下面的代码 解决~~

- (void)setModelArray:(NSArray *)modelArray{
    _modelArray = modelArray;
    [self cleanSubItemView];
    for (id model in modelArray) {
        KapReplyItemView *view = [self createdItemByModel:model];
        [self addSubview:view];
        [self.itemViewsArray addObject:view];
        float height = PCH_BitMap_BY_SIZE(200);
        if(!_lastView){
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self);
                make.left.right.equalTo(self);
                make.height.mas_equalTo(height);
            }];
        }else{
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(_lastView.mas_bottom);
                make.left.right.equalTo(self);
                make.height.mas_equalTo(height);
            }];
        }
        _lastView = view;
    }
}
- (void)changeCommentReplyArray:(NSArray *)array{
    if (self.showButton.selected) {// 展开状态
        self.replyView.modelArray = array;
    }else{
        self.replyView.modelArray = @[];
    }
    if (!self.replyView.lastView) {
        [self.replyView mas_remakeConstraints:^(MASConstraintMaker *make) {
            float topOffset = PCH_BitMap_BY_SIZE(30);
            make.top.equalTo(self.contentLabel.mas_bottom).offset(topOffset);
            make.left.equalTo(self.headerImageView);
            make.right.equalTo(self.titleLabel);
            make.bottom.equalTo(self.replyView.lineView);
        }];
        return;
    }
    [self.replyView mas_remakeConstraints:^(MASConstraintMaker *make) {
        float topOffset = PCH_BitMap_BY_SIZE(30);
        make.top.equalTo(self.contentLabel.mas_bottom).offset(topOffset);
        make.left.equalTo(self.headerImageView);
        make.right.equalTo(self.titleLabel);
        make.bottom.equalTo(self.replyView.lastView);
    }];
}

进一步优化,降低了一下耦合

#import "KapReplyView.h"
@property (nonatomic,copy) void (^customBlock)(MASConstraintMaker *make);
// 重写了mas_make
- (NSArray *)mas_makeConstraints:(void (^)(MASConstraintMaker *))block{
    self.customBlock = block;
    return [super mas_makeConstraints:^(MASConstraintMaker *make) {
        block(make);
        make.bottom.equalTo(self.lineView);
    }];
}
// 修改了下面的
- (void)setModelArray:(NSArray *)modelArray{
    _modelArray = modelArray;
    [self cleanSubItemView];
    for (id model in modelArray) {
        KapReplyItemView *view = [self createdItemByModel:model];
        [self addSubview:view];
        [self.itemViewsArray addObject:view];
        float height = PCH_BitMap_BY_SIZE(200);
        if(!_lastView){
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self);
                make.left.right.equalTo(self);
                make.height.mas_equalTo(height);
            }];
        }else{
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(_lastView.mas_bottom);
                make.left.right.equalTo(self);
                make.height.mas_equalTo(height);
            }];
        }
        _lastView = view;
    }
    if (!self.lastView) {
        [self mas_remakeConstraints:^(MASConstraintMaker *make) {
            self.customBlock(make);
            make.bottom.equalTo(self.lineView);
        }];
        return;
    }
    [self mas_remakeConstraints:^(MASConstraintMaker *make) {
        self.customBlock(make);
        make.bottom.equalTo(self.lastView);
    }];
}
#import "KapCommentCell.h"
- (void)changeCommentReplyArray:(NSArray *)array{
    if (self.showButton.selected) {// 展开状态
        self.replyView.modelArray = array;
    }else{
        self.replyView.modelArray = @[];
    }
//    if (!self.replyView.lastView) {
//        [self.replyView mas_remakeConstraints:^(MASConstraintMaker *make) {
//            float topOffset = PCH_BitMap_BY_SIZE(30);
//            make.top.equalTo(self.contentLabel.mas_bottom).offset(topOffset);
//            make.left.equalTo(self.headerImageView);
//            make.right.equalTo(self.titleLabel);
//            make.bottom.equalTo(self.replyView.lineView);
//        }];
//        return;
//    }
//    [self.replyView mas_remakeConstraints:^(MASConstraintMaker *make) {
//        float topOffset = PCH_BitMap_BY_SIZE(30);
//        make.top.equalTo(self.contentLabel.mas_bottom).offset(topOffset);
//        make.left.equalTo(self.headerImageView);
//        make.right.equalTo(self.titleLabel);
//        make.bottom.equalTo(self.replyView.lastView);
//    }];
}

你可能感兴趣的:(mas_updateConstraints的坑)