我需要實現一個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,它們需要時間才能計算出它們的大小以適應其內容。
有沒有辦法做到這一點?