簡體   English   中英

將UIWebView集成到UICollectionView(UIcollectionViewCell)中

[英]Integrating an UIWebView in a UICollectionView(UIcollectionViewCell)

我在將UIWebView集成到UiCollectionView中時遇到了一些問題。

出現主要問題是因為UiCollectionView的委托:-

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

在知道UIWebview的大小之前被調用

- (void)webViewDidFinishLoad:(UIWebView *)webView

在這里,我有2種不同的文本:short_description和long描述,需要將它們保留在UICollectionViewCell的UIWebView中。 當您點擊單元格時,將顯示詳細說明。 再次點擊將顯示簡短說明,依此類推。

在接口中聲明:

@interface ProductViewController ()
{
float heightOfWebView;
float heightOfWebViewExpanded;
BOOL isArticleLoaded;
BOOL secondArticleLoaded;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    isArticleLoaded = NO;
    secondArticleLoaded = NO;
    [self customizeViewDidLoad];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
[cell.webViewDescription loadHTMLString:[NSString stringWithFormat:@"<html><body style=\"background-color: #ffffff; text-align: %@; font-size: %d; font-family: Roboto-Regular; color: #2D2D2D\">%@</body></html>", @"justify" ,14,myMutableString] baseURL: nil];
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
 if (_descriptionExpanded)
 {
  return CGSizeMake(self.view.bounds.size.width - 20, 48+heightOfWebViewExpanded);
 }else{
  return CGSizeMake(self.view.bounds.size.width - 20, 48+heightOfWebView);
 }                                                       
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    if (!isArticleLoaded)
    {
        heightOfWebView = [[webView stringByEvaluatingJavaScriptFromString:@"document.height"] floatValue];
        isArticleLoaded = YES;

        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:3 inSection:0];
        [self.collectionView performBatchUpdates:^{
            [self.collectionView reloadItemsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil]];
        } completion:^(BOOL finished)
         {

         }];
    }
    else if (!secondArticleLoaded && _descriptionExpanded)
    {
        heightOfWebViewExpanded = [[webView stringByEvaluatingJavaScriptFromString:@"document.height"] floatValue];
        secondArticleLoaded = YES;

        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:3 inSection:0];
        [self.collectionView performBatchUpdates:^{
            [self.collectionView reloadItemsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil]];
        } completion:^(BOOL finished)
         {

         }];
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM