问题描述
我知道,有一个类似question,但我想给我是如何试图做到这一点,因为原来的问题没有给予任何建议更多的提示。结果
我有一个的UITableViewCell
的内容查看的子视图有一个 UICollectionView
,我想有单元格高度在的CollectionView contentSize
的功能表视图细胞是集合视图委托和数据源。结果
该集合视图应该是固定不垂直流动滚动的可能性,它应在的细胞数调整其高度。结果
要做到这一点,我试图用我常用的表视图单元格使用相同的技术。我创建了一个假的电池,并保持对它的引用,则在该方法 - (CGFloat的)的tableView:(UITableView的*)的tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
我喂与数据单元,它应该显示,然后要求其高度为COM pressed大小。结果
这样的事情:结果
I know that there is a similar question, but I'd like to give more hints about how I tried to achieve that since the original question doesn't give any advice.
I have a UITableViewCell
as a subview of the contentView there is a UICollectionView
, I'd like to have the cell height in function of the collectionview contentSize
, the table view cell is the collection view delegate and datasource.
The collection view should be fixed without scrolling possibilities in a vertical flow and it should adapt its height on the number of cell lines.
To do that I've tried to used the same technique I use with common table view cells. I create a fake cell and keep a reference to it, then in the method - (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
I feed the cell with the data that it should display and then ask to its height for compressed size.
Something like that:
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
CGSize size = CGSizeZero;
NSDictionary * data = self.data[indexPath.row];
if (data[KEY_CELL_IDENTIFIER] == CellIdentifierPost) {
NSNumber * cachedHeight = [self.heightCaches objectForKey:[(PostObject*)data[KEY_CELL_DATA] postObjectId]];
if (cachedHeight) {
return (CGFloat)[cachedHeight doubleValue];
}
[_heightCell configureCellWith:data[KEY_CELL_DATA]];
size = [_heightCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
[self.heightCaches setObject:@(size.height) forKey:[(PostObject*)data[KEY_CELL_DATA] postObjectId]];
}
else if (data[KEY_CELL_IDENTIFIER] == CellIdentifierComment){
size = (CGSize) {
.width = NSIntegerMax,
.height = 160.f
};
}
else {
size = (CGSize) {
.width = NSIntegerMax,
.height = 50.f
};
}
return size.height;
}
本方法适用于其他单元确实很好,但不是这一个,结果是一个完全融为一体pressed几乎零高度的细胞。结果
这个问题似乎是由于内细胞放置。即使喂养数据后我强迫集合视图重新加载数据时,它似乎永远叫 -collectionView:cellForItemAtIndexPath:
出现这种情况可能是因为仍然没有显示单元。结果
有周围的工作?
This method works really fine for the other cells, but not for this one, the result is a totally compressed cell with almost a zero height.
The problem seems to be due to the inner cells placement. Even if after feeding the data I force the collection view to reload data, it seems to never call the -collectionView:cellForItemAtIndexPath:
this happens probably because the cell is still not displayed.
Is there a work around?
推荐答案
我建议你创建一个高度约束你的CollectionView。
I'd recommend you to create a height constraint for your collectionView.
然后,基于细胞的数量,你可以计算的高度它,并在结束时,你更新的约束,并调用 [的CollectionView setNeedsUpdateConstraints]
Then, based on the number of cells, you may calculate the height for it, and, at the end, you update that constraint and call [collectionView setNeedsUpdateConstraints]
这篇关于一个UITableViewCell内UICollectionVIew如何获得与自动布局动力高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!