[英]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.