Iβm stuck in a combination of WebView and CollectionView, and you are right, on the one hand, in the collection you need to determine the size of the cell, first of all, before displaying the collection itself. ie
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
where, on the other hand, WebView can determine the actual size, which is only required on webViewDidFinishLoad:
HOW TO SOLVE SUCH PROBLEMS?
In my case, I had only one section in which there was a webView. So what I did, I calculated the size in webViewDidFinishLoad: and then reloaded this particular section after it
- (void)webViewDidFinishLoad:(UIWebView *)aWebView { if (!_isArticleLoaded) { CGRect frame = aWebView.frame; frame.size.height = 1; aWebView.frame = frame; CGSize fittingSize = [aWebView sizeThatFits:CGSizeZero]; frame.size = fittingSize; aWebView.frame = frame; NSMutableIndexSet *mutableIndexSet = [[NSMutableIndexSet alloc] init]; [mutableIndexSet addIndex:2]; NSLog(@"size: %f, %f", fittingSize.width, fittingSize.height); _height = [NSNumber numberWithFloat:fittingSize.height]; _isArticleLoaded = YES; [_collectionView reloadSections:mutableIndexSet]; } }
Therefore, initially in viewdidload: current values ββare assigned to local variables:
_isArticleLoaded = NO; _height = [NSNumber numberWithFloat:200.0];
and for UICollectionView I had the following: -
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.section == 2) { return CGSizeMake(self.collectionView.frame.size.width,[_height floatValue]+35); } }
This pretty much solved my problem when I was dealing with one section.
In your case, my friend, you should not use WebView for each cell, it will prove to be expensive