美文网首页
UITableViewCell嵌套UICollectionVie

UITableViewCell嵌套UICollectionVie

作者: 易明 | 来源:发表于2020-08-30 23:38 被阅读0次

我一个UITableViewCell里面嵌套UICollectionView,我需要让UITableViewCell有数据后,给UICollectionView reloadData,得到高度,设置到UICollectionView 的height,就可以把UITableViewCell的高度自动撑开
第一天我始终在一个圈子里绕来绕去
一直在纠结于这个问题


WechatIMG2.png

以至于我总结出三种做法

#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是异步操作,这时候获取的height不准确,可以刷新布局
    CGFloat height1 = self.collectionView.collectionViewLayout.collectionViewContentSize.height;
    [self.collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(height1)).priorityHigh();
    }];
    
    // ========== ========== 第2种做法 ========== ==========
    //代码在主线程执行,能获取到真实高度,不主动刷新布局(需要把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();
    }];
}

但我始终不能满足于此,代码应该有更优雅的方式
最后得到网友的帮助,终于得到了一个较为不错的办法
废话不多说,上代码


@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
{
    // 先对bgview进行布局,这里需对bgView布局后collectionView宽度才会准确
    self.bgView.frame = CGRectMake(0, 0, targetSize.width, 44);
    [self.bgView layoutIfNeeded];
    
    // 在对collectionView进行布局
    self.collectionView.frame = CGRectMake(0, 0, targetSize.width, 44);
    [self.collectionView layoutIfNeeded];
    
    // 由于这里collection的高度是动态的,这里cell的高度我们根据collection来计算
    CGSize collectionSize = self.collectionView.collectionViewLayout.collectionViewContentSize;
    CGFloat cotentViewH = collectionSize.height;
    
    return CGSizeMake([UIScreen mainScreen].bounds.size.width, cotentViewH + 30);
}

最后我也不懂这是为什么但效果是完美的
附上地址
https://github.com/ganfuchuan/test
应该还有更优雅的做法吧,不过我暂时还不会

相关文章

网友评论

      本文标题:UITableViewCell嵌套UICollectionVie

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