UITableViewCell嵌套UICollectionView動(dòng)態(tài)高度

我一個(gè)UITableViewCell里面嵌套UICollectionView,我需要讓UITableViewCell有數(shù)據(jù)后,給UICollectionView reloadData,得到高度,設(shè)置到UICollectionView 的height,就可以把UITableViewCell的高度自動(dòng)撐開
第一天我始終在一個(gè)圈子里繞來繞去
一直在糾結(jié)于這個(gè)問題


WechatIMG2.png

以至于我總結(jié)出三種做法

#import "UCCollectionTableViewCell.h"
#import "FCCollectionViewCell.h"

@interface UCCollectionTableViewCell()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UICollectionView *collectionView;
@end

@implementation UCCollectionTableViewCell

- (void)setInfo:(NSDictionary *)info {
    _info = info;
    self.titleLabel.text = info[@"title"];

    [self.collectionView reloadData];
    [self.collectionView layoutIfNeeded];
    
    // ========== ========== 第1種做法 ========== ==========
    //因reloadData是異步操作,這時(shí)候獲取的height不準(zhǔn)確,可以刷新布局
    CGFloat height1 = self.collectionView.collectionViewLayout.collectionViewContentSize.height;
    [self.collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(height1)).priorityHigh();
    }];
    
    // ========== ========== 第2種做法 ========== ==========
    //代碼在主線程執(zhí)行,能獲取到真實(shí)高度,不主動(dòng)刷新布局(需要把cell滑出屏幕再滑回來才能顯示正確的布局)
    dispatch_async(dispatch_get_main_queue(), ^{
        CGFloat height2 = self.collectionView.collectionViewLayout.collectionViewContentSize.height;
        [self.collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(@(height2)).priorityHigh();
        }];
    });
    
    // ========== ========== 第3種做法 ========== ==========
    NSArray *list = info[@"list"];
    CGFloat height3 = ((list.count-1)/3 + 1) * 40 + 10;
    [self.collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(height3)).priorityHigh();
    }];
}

但我始終不能滿足于此,代碼應(yīng)該有更優(yōu)雅的方式
最后得到網(wǎng)友的幫助,終于得到了一個(gè)較為不錯(cuò)的辦法
廢話不多說,上代碼


@interface UCCollectionTableViewCell()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIView *bgView;

@end

@implementation UCCollectionTableViewCell

- (void)setInfo:(NSDictionary *)info {
    _info = info;
    
    self.titleLabel.text = info[@"title"];
    
    [self.collectionView reloadData];
    [self.collectionView layoutIfNeeded];
    [self.bgView layoutIfNeeded];
        
}

- (CGSize)systemLayoutSizeFittingSize:(CGSize)targetSize withHorizontalFittingPriority:(UILayoutPriority)horizontalFittingPriority verticalFittingPriority:(UILayoutPriority)verticalFittingPriority
{
    // 先對(duì)bgview進(jìn)行布局,這里需對(duì)bgView布局后collectionView寬度才會(huì)準(zhǔn)確
    self.bgView.frame = CGRectMake(0, 0, targetSize.width, 44);
    [self.bgView layoutIfNeeded];
    
    // 在對(duì)collectionView進(jìn)行布局
    self.collectionView.frame = CGRectMake(0, 0, targetSize.width, 44);
    [self.collectionView layoutIfNeeded];
    
    // 由于這里collection的高度是動(dòng)態(tài)的,這里cell的高度我們根據(jù)collection來計(jì)算
    CGSize collectionSize = self.collectionView.collectionViewLayout.collectionViewContentSize;
    CGFloat cotentViewH = collectionSize.height;
    
    return CGSizeMake([UIScreen mainScreen].bounds.size.width, cotentViewH + 30);
}

最后我也不懂這是為什么但效果是完美的
附上地址
https://github.com/ganfuchuan/test
應(yīng)該還有更優(yōu)雅的做法吧,不過我暫時(shí)還不會(huì)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容