UICollectionViewCell 在滚动时消失

UICollectionViewCell disappears while scrolling

我正在使用 uicollectionview 创建类似滚动视图的旋转木马。我认为 uicollectionview 是这个用例的最佳方法。我能够在 uicollectionview 内渲染 uicollectionviewcell 但问题是,当我滚动 uicolectionview 时,后面的单元格将消失,最后所有单元格都变黑了。另一个问题是单元格只有在其边界完全位于轮播内时才会加载。

这就是我在 cellForItemAtIndexPath 委托方法

中设置 uicollectionviewcell 的方式
NSString *urlString = [NSString stringWithFormat:BASE_URL, panoid, heading, pitch];
NSURL *panoUrl = [NSURL URLWithString:urlString];

//WIDTH calculated based on screen size, roughly about three cells per screen.
CGFloat WIDTH = collectionView.frame.size.width / 3;

UIImageView *panoImg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 133.3)];
// add padding to imageview
panoImg.bounds = CGRectInset(panoImg.frame, 2, 2);
// do I need GCD to do async process here? the images are all loaded from urls. not sure if this will cause a problem.
dispatch_async(dispatch_get_global_queue(0, 0), ^{
    NSData *data = [NSData dataWithContentsOfURL:panoUrl];
    dispatch_async(dispatch_get_main_queue(), ^{
        [panoImg setImage:[UIImage imageWithData:data]];
    });
});
seeInsideCollectionViewCell *cell = [_panoCarousel dequeueReusableCellWithReuseIdentifier:@"panoCarouselCell" forIndexPath:indexPath];
cell.frame = CGRectMake(indexPath.item * WIDTH + 4, 0, WIDTH, 133.3);
cell.backgroundColor = [UIColor blackColor];
[cell addSubview: panoImg];

UILabel *label = [[UILabel alloc] initWithFrame: CGRectMake(4, 4, WIDTH, 15)];
[label setBackgroundColor: [UIColor colorWithRed:0.63 green:0.63 blue:0.63 alpha:0.5]];
[label setFont: [UIFont fontWithName:@"Roboto-Light" size:10.0]];
[label setText: _BussinessViewsNames[indexPath.item]];
[cell addSubview:label];

return cell;

有什么建议,单元格都加载了,只是加载不是无缝流畅,我附上了一个gif来演示这里的问题。

删除此行,因为您不应更改单元格的框架

cell.frame = CGRectMake(indexPath.item * WIDTH + 4, 0, WIDTH, 133.3);

当单元格可重用时,它已经有 uiimageview,你再次创建新的 uiimageview 并添加它,单元格将有多个 uiimageviews 作为子视图

在另一个 class 或类似

的方法中移动加载图像

self.imageLoader

 -(void)loadImageWithURL:(NSURL *)url successBlock:(void (^)(UIImage *image, id itemId))successBlock failureBlock:(void (^)(NSError *error))failureBlock

并在 cellForItem 方法中调用它(检查获取的图像的 id)