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的每个单元格,它会证明昂贵