4

を実装する必要がありUICollectionViewます。私が使用する細胞は、UIWebViewsそれらの中にあります。主な問題は、セルのサイズを webViews 内の実際のコンテンツに合わせたいことです。

次の2つの方法を使用しました。

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

UICollectionViewLayoutwebview がそのコンテンツをロードした後にのみ、特定のセルのサイズを調整することは何とか可能ですか?

私が見つけたすべての例は、サイズが固定されているか、コンテンツに合わせてサイズが計算されるまで時間がかかる webViews を使用していませんでした。

これを達成する方法はありますか?

4

2 に答える 2

0

私は WebView と CollectionView の組み合わせで立ち往生していました。その通りです。一方、collectionview は最初に、つまり collectionView 自体を表示する前に、セルのサイズを決定する必要があります。

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

一方、WebView は実際に必要なサイズを決定できます。webViewDidFinishLoad:

このような問題を解決するには?

私の場合、webView を含むセクションが 1 つしかなかったので、サイズを計算して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 に答える