ios - 重复使用 UICollectionView 中的单元格时,会短暂显示重复使用的 UICollectionViewCell 的旧内容

标签 ios ios6 uicollectionview

我正在使用 UICollectionView 来显示从 URL 异步加载的图片网格。我的 Collection View 使用可重复使用的单元格来显示 UICollectionViewCells。当单元格未被重复使用时,一切都会正确显示,但当我稍微滚动时,重复使用的单元格在开始正常行为之前会短暂闪烁旧内容。

这是自定义 UICollectionViewController 的实现:

#import "MyCollectionViewViewController.h"
#import "MyCollectionViewCell.h"

@interface MyCollectionViewViewController ()    
@property (strong, nonatomic) NSArray *data;    
@end

@implementation MyCollectionViewViewController

@synthesize data = _data;

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.data = @[
        @"http://s3-ec.buzzfed.com/static/enhanced/webdr01/2013/1/18/12/enhanced-buzz-wide-851-1358529670-4.jpg",
        @"http://s3-ec.buzzfed.com/static/enhanced/webdr03/2013/1/18/11/enhanced-buzz-wide-26311-1358526816-5.jpg",
        @"http://s3-ec.buzzfed.com/static/enhanced/webdr03/2013/1/18/11/enhanced-buzz-wide-26311-1358527190-11.jpg",
        @"http://s3-ec.buzzfed.com/static/enhanced/webdr02/2013/1/18/11/enhanced-buzz-wide-7517-1358526694-11.jpg",
        @"http://s3-ec.buzzfed.com/static/enhanced/webdr03/2013/1/18/11/enhanced-buzz-wide-1965-1358527802-7.jpg",
        @"http://s3-ec.buzzfed.com/static/enhanced/webdr03/2013/1/18/11/enhanced-buzz-wide-2165-1358527708-14.jpg",
        @"http://s3-ec.buzzfed.com/static/enhanced/webdr03/2013/1/18/11/enhanced-buzz-wide-1965-1358527894-12.jpg",
        @"http://s3-ec.buzzfed.com/static/enhanced/webdr02/2013/1/18/12/enhanced-buzz-wide-15957-1358529198-18.jpg",
        @"http://s3-ec.buzzfed.com/static/enhanced/webdr02/2013/1/18/12/enhanced-buzz-wide-16520-1358528981-9.jpg",
        @"http://s3-ec.buzzfed.com/static/enhanced/webdr02/2013/1/18/12/enhanced-buzz-wide-16517-1358529292-5.jpg",
        @"http://s3-ec.buzzfed.com/static/enhanced/webdr01/2013/1/18/12/enhanced-buzz-wide-20349-1358529323-20.jpg",
        @"http://s3-ec.buzzfed.com/static/enhanced/webdr01/2013/1/18/12/enhanced-buzz-wide-32444-1358529959-9.jpg",
        @"http://s3-ec.buzzfed.com/static/enhanced/webdr01/2013/1/18/12/enhanced-buzz-wide-32343-1358530043-7.jpg",
        @"http://s3-ec.buzzfed.com/static/enhanced/webdr02/2013/1/18/12/enhanced-buzz-wide-23646-1358530321-2.jpg",
        @"http://s3-ec.buzzfed.com/static/enhanced/webdr01/2013/1/18/12/enhanced-buzz-wide-28223-1358530801-15.jpg",
        @"http://s3-ec.buzzfed.com/static/enhanced/webdr01/2013/1/18/12/enhanced-buzz-wide-32273-1358530695-16.jpg",
        @"http://s3-ec.buzzfed.com/static/enhanced/webdr01/2013/1/18/12/enhanced-buzz-wide-31288-1358531103-16.jpg"
    ];

    [self.collectionView registerNib:[UINib nibWithNibName:@"MyCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"myView"];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    NSString *urlString = [self.data objectAtIndex:indexPath.item];

    MyCollectionViewCell *myView = [collectionView dequeueReusableCellWithReuseIdentifier:@"myView" forIndexPath:indexPath];
    myView.urlString = urlString;

    [myView setNeedsDisplay];

    return myView;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(150, 150);
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.data.count;
}

@end

这里是自定义 UICollectionViewCell 的实现。

#import "MyCollectionViewCell.h"
#import <QuartzCore/QuartzCore.h>
#import <AVFoundation/AVFoundation.h>

@interface MyCollectionViewCell ()

@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@end

@implementation MyCollectionViewCell

@synthesize urlString = _urlString;

+ (void)loadAsyncImageFromURL:(NSString *)urlString withCallback:(void (^)(UIImage *))callback {
    NSURL *url = [NSURL URLWithString:urlString];

    dispatch_queue_t downloadQueue = dispatch_queue_create("com.example.downloadqueue", NULL);
    dispatch_async(downloadQueue, ^{
        NSData * imageData = [NSData dataWithContentsOfURL:url];
        UIImage *image = [UIImage imageWithData:imageData];

        dispatch_async(dispatch_get_main_queue(), ^{
            callback(image);
        });
    });
}

-(void) fadeInView:(UIView *)view WithSecondDuration:(double)duration {
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"opacity"];
    animation.beginTime = 0;
    animation.duration = duration;
    animation.fromValue = [NSNumber numberWithFloat:0.0f];
    animation.toValue = [NSNumber numberWithFloat:1.0f];
    animation.removedOnCompletion = NO;
    animation.fillMode = kCAFillModeBoth;
    animation.additive = NO;
    [view.layer addAnimation:animation forKey:@"opacityIN"];
}

- (void) setUrlString:(NSString *)urlString {
    _urlString = urlString;

    self.imageView.image = nil;
    self.imageView.layer.opacity = 0;

    if (urlString) {
        [MyCollectionViewCell loadAsyncImageFromURL:urlString withCallback:^(UIImage *image) {
            self.imageView.image = image;
            [self fadeInView:self.imageView WithSecondDuration:.25];
        }];
    }
}


@end

最佳答案

您可以在 UICollectionViewCell 子类中实现 -(void)prepareForReuse。并在那里进行重置。

关于ios - 重复使用 UICollectionView 中的单元格时,会短暂显示重复使用的 UICollectionViewCell 的旧内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16289525/

相关文章:

ios - 如何正确判断 Destination View Controller 类

objective-c - 与 iOS 6.0 原生 Facebook 集成共享 : "Posted via my app name"?

objective-c - 从一个 UICollectionViewLayout 切换到另一个时的自定义动画?

swift - 如何清除TableView并重新加载数据?

iphone - 调试程序收到信号 "SIGABRT"

ios - [MyClassName copyWithZone :]: unrecognized selector sent to instance?

iphone - 在 iPhone 上使用 &lt;iframe&gt; 播放 YouTube 失败,但在 iOS 6 下的 iPad 上仍然可以使用(两者在 iOS 5 上都可以正常工作)

ios - 如何在 Objective C 中获取枚举的数值,或用数值设置枚举?

ios - 无法在 iOS 中保存应用内购买

ios - 与 iPhone 3G 和 iPhone 5 兼容的应用程序