- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { LRPatientCommentCell *cell = [tableView dequeueReusableCellWithIdentifier:CellID]; //下面两句是关键 cell.frame = tableView.bounds; [cell layoutIfNeeded]; cell.item = self.array[indexPath.row]; return cell; } - (void)setItem:(LRPatientCommentItem *)item { _item = item; // 标签 [self.collectionView reloadData]; [self layoutSubviews];//这句是关键 } //这里获取collectionView 显示高度进行更新 - (void)layoutSubviews { [super layoutSubviews]; CGFloat height = self.collectionView.collectionViewLayout.collectionViewContentSize.height; [self.collectionView mas_updateConstraints:^(MASConstraintMaker *make) { make.height.mas_equalTo(height); }]; NSLog(@"height %f",height); }
参考
https://blog.csdn.net/zhanglizhi111/article/details/86292634