(1) cell的重用引发的问题

2017-08-17  本文已影响23人  活最好的自己

1. 重用时下载图片异常

1> 如果只在某些 cell 中使用 sdwebimage ,则有可能会导致重用引起的异常

    if (isCustomRoom)
    {
        NSURL *url = [NSURL URLWithString:imageUrl];
        WeakSelf
        [_customRoomImageView sd_setImageWithURL:url completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            if (image)
            {
                [weakSelf _updateCustomActivityRoom:YES];
            }
            else
            {
                [weakSelf _updateCustomActivityRoom:NO];
            }
        }];
    }
    else
    {
        [self _updateCustomActivityRoom:NO];
    }

2> 若改成每个 cell 都使用 sdwebimage 下载图片,则会避免此问题

    BOOL isCustomRoom = (accessoryType == CellAccessoryTypeCustomSkin);
    imageUrl = isCustomRoom ? imageUrl : @"";
    NSURL *url = [NSURL URLWithString:imageUrl];
    
    WeakSelf
    [_customRoomImageView sd_setImageWithURL:url completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        if (image)
        {
            [weakSelf _updateCustomActivityRoom:YES];
        }
        else
        {
            [weakSelf _updateCustomActivityRoom:NO];
        }
    }];

2. 重用时更新布局异常

1> 在 cell 中显示不同的标签图片时,使用更新约束的代码,结果宽度总是会重用...

2> 而且第一个方法的图片宽度是对的,第二个方法的图片宽度达到最大值;当再次滑到第一个方法的图片是,宽度也被感染成最大值了.

/// 分类标签
- (void)setDefaultTagTitle:(NSString *)title{
    [_tagBtn setTitle:title forState: UIControlStateNormal];
    CGFloat width = [self widthOfRoomClassifyView:title]  ;
    [_tagBtn mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(@(width));
        make.centerX.mas_equalTo(_briefImageView);
    }];
}

/// 运营标签
- (void)setOperationTagTitle:(NSString *)title{
    [_tagBtn setTitle:title forState: UIControlStateNormal];
    CGFloat width = [self widthOfRoomClassifyView:title]  ;
    [_tagBtn mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(@(width));
        make.left.mas_equalTo(_briefImageView);
    }];
}

3> 不得已,把两个类型的图片都改成使用第一个方法更新宽度,结果没有重用导致的异常.

4> 对比两个方法,唯一的差别就是自动布局里面的make.centerX和make.left,又想了想mas_update的原理是更新约束的 offset 值.

5> 要想改依赖的对象,必须使用 mas_remake,接下来问题就迎刃而解了.

/// 分类标签
- (void)setDefaultTagTitle:(NSString *)title{
    [_tagBtn setTitle:title forState: UIControlStateNormal];
    CGFloat width = [self widthOfRoomClassifyView:title]  ;
    [_tagBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(@(width));
        make.centerX.mas_equalTo(_briefImageView);
        make.top.mas_equalTo(_briefImageView.mas_top).offset(-kImgViewTagTop);
        make.height.mas_equalTo(@(kTagViewHeight));
    }];
}

/// 运营标签
- (void)setOperationTagTitle:(NSString *)title{
    [_tagBtn setTitle:title forState: UIControlStateNormal];
    CGFloat width = [self widthOfRoomClassifyView:title]  ;
    [_tagBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(@(width));
        make.left.mas_equalTo(_briefImageView);
        make.top.mas_equalTo(_briefImageView.mas_top);
        make.height.mas_equalTo(@(kTagViewHeight));
    }];
}
上一篇下一篇

猜你喜欢

热点阅读