美文网首页
UITableViewCell 根据数据动态适应高度的坑

UITableViewCell 根据数据动态适应高度的坑

作者: 洪哥 | 来源:发表于2017-04-26 14:28 被阅读19次

项目中经常遇到在 UITableViewCell 中嵌套 其他 UITableView 或者 UICollectionView 的情况. 在设置了嵌套View的高度后, cell 的高度异常问题.

示例图.png
- (void)awakeFromNib {
    [super awakeFromNib];

    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    
    NSString *identifier = NSStringFromClass([XXXCollectionViewCell class]);
    UINib *xib = [UINib nibWithNibName:identifier bundle:[NSBundle mainBundle]];
    
    [self.collectionView registerNib:xib forCellWithReuseIdentifier:identifier];
    
    @weakify(self);
    [RACObserve(self.collectionView, contentSize) subscribeNext:^(id x) {
        @strongify(self);
        CGFloat height = self.collectionView.contentSize.height;
        self.contentConstraintHeight.constant = height;
    }];
}

- (void)loadData:(id)cellData
{
    [self.collectionView reloadData];
    
    [self layoutIfNeeded];
}

关键是 reloadData后的 [self layoutIfNeeded];

相关文章

网友评论

      本文标题:UITableViewCell 根据数据动态适应高度的坑

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