0

I had some problems with integrating an UIWebView in a UiCollectionView.

The main problem appeared because the delegate of UiCollectionView: -

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

is called before knowing the size of the UIWebview

- (void)webViewDidFinishLoad:(UIWebView *)webView

Here i have 2 different texts: short_description and long Description that needs to be kept in an UIWebView in a UICollectionViewCell. When you tap on the cell the Long description Will appear. Tap again short description will appear and so on.

Ekta Padaliya
  • 5,743
  • 3
  • 39
  • 51
Gabriel
  • 1,215
  • 13
  • 16

1 Answers1

0

declare this in the interface:

@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)
         {

         }];
    }
}
sandy
  • 1,072
  • 2
  • 12
  • 31
Gabriel
  • 1,215
  • 13
  • 16