我需要实现一个UICollectionView
. 我使用的细胞UIWebViews
在它们里面。主要问题是我想让单元格大小适应 webViews 中的实际内容。
我使用了以下两种方法:
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
CollectionViewCellDefault *cell = (CollectionViewCellDefault *)[collectionView cellForItemAtIndexPath:indexPath];
NSLog(@"rect %@", NSStringFromCGRect(cell.wvDisplayValue.frame));
CGSize retval = cell.wvDisplayValue.frame.size;
retval.height += 135;
retval.width += 135;
return retval;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
CollectionViewCellDefault *cell = [collectionView dequeueReusableCellWithReuseIdentifier:detailColumnsCell forIndexPath:indexPath];
NSURL *indexFileURL = [[NSBundle mainBundle] URLForResource:@"index" withExtension:@"html"];
[cell.wvDisplayValue loadRequest:[NSURLRequest requestWithURL:indexFileURL]];
cell.lblTitle.text = @"Description: ";
cell.frame = cell.wvDisplayValue.frame;
return cell;
}
问题当然是 webView 只有在加载后才知道它的大小:
- (void)webViewDidFinishLoad:(UIWebView *)webView
仅在 webview 加载其内容后,是否可以通过某种方式调整UICollectionViewLayout
特定单元格的大小?
我发现的每个示例要么具有固定大小,要么不使用 webView,这需要时间才能计算出适合其内容的大小。
有没有办法做到这一点?