4

我需要实现一个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,这需要时间才能计算出适合其内容的大小。

有没有办法做到这一点?

4

2 回答 2

0

您可以为 webViewDidFinishLoad 中的相应 webView 尝试此操作:

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

这将调整 webView 的大小以适应其内容。然后计算新的布局并调用 invalidateLayout 来使用新的布局。

于 2013-06-13T19:55:54.890 回答
0

我被困在 WebView 和 CollectionView 组合中,你是对的,一方面,collectionview 需要先确定单元格的大小,即在显示 collectionView 本身之前,即

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

另一方面,WebView 可以确定它只需要的实际大小webViewDidFinishLoad:

如何解决此类问题?

好吧,就我而言,我只有一个部分包含 webView,所以我所做的是计算大小,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,它会被证明是昂贵的

于 2015-04-29T12:49:45.230 回答