UICollectionView with custom UICollectionViewCell?

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
AnnouncementCollectionViewCell *cell = (AnnouncementCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:kCollectionViewCellIdentifier forIndexPath:indexPath];

NSURL *url = [NSURL fileURLWithPath: fileName isDirectory: NO];
[cell.announcement setDelegate:self];

[cell.announcement loadRequest:[NSURLRequest requestWithURL:url]];

int contentHeight = [[cell.announcement stringByEvaluatingJavaScriptFromstring: @"document.body.offsetHeight"] integerValue];

NSLog(@"Content Height: %d", contentHeight);

CGRect rect = cell.announcement.frame;
rect.size.height = contentHeight;
cell.announcement.frame = rect;

cell.autoresizesSubviews = YES;
cell.frame = rect;

return cell;

When you populate the collection data
the webView still does not have time to download the content starts vyschityvanii the height of the element
How to make that first loaded the content then was calculated the height of the element.
If you use dispatch_sync, the app crashes when loading the collection
September 26th 19 at 06:13
1 answer
September 26th 19 at 06:15
Don't touch the height in cellForItemAtIndexPath at all. Only after the data is loaded in the UIWebView to change the height of this webView. When this happens, just call - (void)reloadItemsAtIndexPaths:(NSArray *)indexPaths in order indexPath corresponding to this webView. This will update the contents of the desired cell and will request the size of the delegate.

If you are using UICollectionViewDelegateFlowLayout nuinui sizes give in - (CGSize)collectionView:(UICollectionView *)aCollectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath. This method will be called after the call reloadItemsAtIndexPaths
In this approach, the cells don't even appear. I suspect that the page loads asynchronously. - Myrtie70 commented on September 26th 19 at 06:18

Find more questions by tags iOS