auto-sizing UICollectionView in UITableView with pure code

Viewed 24

I am trying to create one UITableView with header and UITableViewCell header is fixed size. and in each UITableViewCell I will create one UICollectionView. the UICollectionViewCell height is not fixed. so Here I want UICollectionViewCell is auto-sizing in UICollectionView and UICollectionView is auto-sizing in UITableViewCell.

But The height is not auto-sizing after implementation. Could you help me on this.

TableView init:

    - (UITableView *)tableView
    {
        if (_tableView == nil) {
            CGFloat top = 20;
            _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
            _tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
            _tableView.delegate = self;
            _tableView.separatorStyle = 0;
            _tableView.dataSource = self;
            _tableView.estimatedRowHeight = 200;
            _tableView.sectionHeaderHeight = 40;
            _tableView.rowHeight = UITableViewAutomaticDimension;
            _tableView.backgroundColor = [UIColor whiteColor];
            
            
        }
        return _tableView;
    }

UICollectionView in UITableViewCell Init:

    _collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:flowLayout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.bounces = NO;
    _collectionView.showsHorizontalScrollIndicator = NO;
    _collectionView.showsVerticalScrollIndicator = NO;
    _collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView.translatesAutoresizingMaskIntoConstraints = NO;
    [_collectionView registerClass:[KCollectionViewCell class] forCellWithReuseIdentifier:@"KCollectionViewCell"];
    [self.contentView addSubview:_collectionView];
    [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(self.contentView);
        make.left.equalTo(self.contentView).offset(20);
        make.right.equalTo(self.contentView).offset(-20);
    }];

UICollectionViewCell init:

-(void)initUI{
    
    self.label = [[UILabel alloc] init];
    self.label.text = @"aaaaaaaaaaaaaaaa";
    self.label.textColor = XUIColorMain;
    self.label.textAlignment = NSTextAlignmentCenter;
    self.label.layer.borderColor =[UIColor orangeColor].CGColor;
    self.label.clipsToBounds = YES;
    [self.label sizeToFit];
}

- (UICollectionViewLayoutAttributes *)preferredLayoutAttributesFittingAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes {
    UICollectionViewLayoutAttributes *attributes = [super preferredLayoutAttributesFittingAttributes:layoutAttributes];
    CGRect frame = [self.label.text boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, 30) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:[NSDictionary dictionaryWithObjectsAndKeys:self.label.font,NSFontAttributeName, nil] context:nil];
    
    frame.size.width += 10;
    frame.size.height = 30;

    attributes.frame = frame;
    
    return attributes;
}
0 Answers
Related