I know that there is a similar question , but I would like to give more hints about how I tried to achieve this, since the original question donβt give any advice.
I have UITableViewCellas a subset of the contentView there UICollectionView, I would like the height of the cell was in the collectionview function contentSize, the cell of the table view is the delegate of the collection view and the data source. <w> The view of the collection should be fixed without the possibility of scrolling in the vertical stream, and it should adapt its height to the number of cell lines.
For this, I tried to use the same technique from that for ordinary table cells. I am creating a fake cell and stores a reference to it, then in the method- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPathI pass the cell the data that it should display, and then ask its height for the 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;
}
This method works great for other cells, but not for this, the result is a fully compressed cell with almost zero height. The problem seems to be related to the placement of internal cells. Even if, after submitting the data, I forcefully collect the view to reload the data, it seems that it never calls -collectionView:cellForItemAtIndexPath:, this is probably because the cell is still not displayed.
Is there any work around?
source
share