4

我需要實現一個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,它們需要時間才能計算出它們的大小以適應其內容。

有沒有辦法做到這一點?

回答

0

你可以在webViewDidFinishLoad嘗試一下本作相應的web視圖:

CGRect frame = webView.frame; 
CGSize fittingSize = [webView sizeThatFits:CGSizeZero]; 
frame.size = fittingSize; 
webView.frame = frame; 

這將調整webView的,以適應其內容。然後計算新佈局並調用invalidateLayout以使用新佈局。

0

我被卡在WebView中和的CollectionView結合,你是對的,一方面的CollectionView需要確定單元的顯示的CollectionView之前,首先即自身的大小,即

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath

,其中作爲另一方面WebView可以確定它只需要的實際大小webViewDidFinishLoad:

如何解決這些問題?

在我的情況

嘛,我剛一節其中有web視圖中,所以我所做的是我計算的大小webViewDidFinishLoad:,然後重新加載該特定部分後

- (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]; 

    } 

} 

所以最初,在viewdidload:在localVariables分配流動值:

_isArticleLoaded = NO; 

_height = [NSNumber numberWithFloat:200.0]; 

和UICollectionView我以下內容: -

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (indexPath.section == 2) 
    { 
     return CGSizeMake(self.collectionView.frame.size.width,[_height floatValue]+35); 
    } 
} 

這幾乎解決了我的問題,當我正在處理一個部分。

在你的情況下,我的朋友,你不應該使用WebView的每個單元格,它會證明昂貴