0

У меня возникли проблемы с интеграцией UIWebView в UiCollectionView.Интеграция UIWebView в UICollectionView (UIcollectionViewCell)

Основная проблема возникла потому, что делегат UiCollectionView: -

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

вызывается перед тем, зная размер UIWebview

- (void)webViewDidFinishLoad:(UIWebView *)webView 

Здесь у меня есть 2 разные тексты: SHORT_DESCRIPTION и длинное описание, что должен храниться в UIWebView в UICollectionViewCell. Когда вы нажмете на ячейку, появится длинное описание. Нажмите снова, появится краткое описание и т. Д.

ответ

0

заявить об этом в интерфейсе:

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

     }]; 
    } 
}