正确答案是可以,您可以这样做。
几周前我遇到了这个问题。它实际上比你想象的要容易。将您的单元格放入 NIB(或故事板)并固定它们以让自动布局完成所有工作
给定以下结构:
表格视图
TableViewCell
集合视图
CollectionViewCell
CollectionViewCell
CollectionViewCell
[...可变数量的单元格或不同的单元格大小]
解决方案是告诉自动布局首先计算 collectionViewCell 大小,然后是 collection view contentSize,并将其用作单元格的大小。这是“施展魔法”的 UIView 方法:
-(void)systemLayoutSizeFittingSize:(CGSize)targetSize
withHorizontalFittingPriority:(UILayoutPriority)horizontalFittingPriority
verticalFittingPriority:(UILayoutPriority)verticalFittingPriority
您必须在此处设置 TableViewCell 的大小,在您的情况下是 CollectionView 的 contentSize。
CollectionViewCell
在 CollectionViewCell 处,您必须在每次更改模型时告诉单元格进行布局(例如:您设置带有文本的 UILabel,然后单元格必须再次进行布局)。
- (void)bindWithModel:(id)model {
// Do whatever you may need to bind with your data and
// tell the collection view cell's contentView to resize
[self.contentView setNeedsLayout];
}
// Other stuff here...
TableViewCell
TableViewCell 具有魔力。它有一个到您的 collectionView 的出口,使用 UICollectionViewFlowLayout 的 estimatedItemSize 启用 collectionView 单元格的自动布局。
然后,诀窍是在 systemLayoutSizeFittingSize... 方法中设置 tableView 单元格的大小。 (注意:iOS8 或更高版本)
注意:我尝试使用 tableView -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 的委托单元格高度方法。但是 为时已晚自动布局系统计算 CollectionView contentSize 有时您可能会发现调整大小错误的单元格.
@implementation TableCell
- (void)awakeFromNib {
[super awakeFromNib];
UICollectionViewFlowLayout *flow = (UICollectionViewFlowLayout *)self.collectionView.collectionViewLayout;
// Configure the collectionView
flow.minimumInteritemSpacing = ...;
// This enables the magic of auto layout.
// Setting estimatedItemSize different to CGSizeZero
// on flow Layout enables auto layout for collectionView cells.
// https://developer.apple.com/videos/play/wwdc2014-226/
flow.estimatedItemSize = CGSizeMake(1, 1);
// Disable the scroll on your collection view
// to avoid running into multiple scroll issues.
[self.collectionView setScrollEnabled:NO];
}
- (void)bindWithModel:(id)model {
// Do your stuff here to configure the tableViewCell
// Tell the cell to redraw its contentView
[self.contentView layoutIfNeeded];
}
// THIS IS THE MOST IMPORTANT METHOD
//
// This method tells the auto layout
// You cannot calculate the collectionView content size in any other place,
// because you run into race condition issues.
// NOTE: Works for iOS 8 or later
- (CGSize)systemLayoutSizeFittingSize:(CGSize)targetSize withHorizontalFittingPriority:(UILayoutPriority)horizontalFittingPriority verticalFittingPriority:(UILayoutPriority)verticalFittingPriority {
// With autolayout enabled on collection view's cells we need to force a collection view relayout with the shown size (width)
self.collectionView.frame = CGRectMake(0, 0, targetSize.width, MAXFLOAT);
[self.collectionView layoutIfNeeded];
// If the cell's size has to be exactly the content
// Size of the collection View, just return the
// collectionViewLayout's collectionViewContentSize.
return [self.collectionView.collectionViewLayout collectionViewContentSize];
}
// Other stuff here...
@end
TableViewController
记得在你的 TableViewController 中为 tableView 单元格启用自动布局系统:
- (void)viewDidLoad {
[super viewDidLoad];
// Enable automatic row auto layout calculations
self.tableView.rowHeight = UITableViewAutomaticDimension;
// Set the estimatedRowHeight to a non-0 value to enable auto layout.
self.tableView.estimatedRowHeight = 10;
}
信用:@rbarbera 帮助解决了这个问题