我需要实现一个UICollectionView
。我使用的单元格内有UIWebViews
。主要问题是我想让单元大小适应webViews内的实际内容。UICollectionView与UIWebView大小问题
我用以下两种方法:
- (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;
}
问题当然是web视图只知道一旦它被载入它的大小:
- (void)webViewDidFinishLoad:(UIWebView *)webView
是它在某种程度上可以调整大小只有在webview加载其内容后,才能使用特定单元格的UICollectionViewLayout
?
我发现的每个例子都有固定的大小,或者没有使用webViews,它们需要时间才能计算出它们的大小以适应其内容。
有没有办法做到这一点?