HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – 重用UICollectionView中的单元格时,会短暂显示重用的UICollectionViewCell的旧内容大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用UICollectionView来显示从URL异步加载的@L_772_1@网格.我的集合视图使用可重用单元格来显示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

解决方法

您可以尝试在单元格完成显示时将单元格的URL设置为nil,它可以在我的项目中工作.
-(void)collectionView:(UICollectionView *)cv didEndDisplayingCell:(UICollectionViewCell *)cell forItemATindexPath:(NSIndexPath *)indexPath
{
    [(MyCollectionViewCell *)cell setUrlString:nil];
}

我希望这有帮助!

大佬总结

以上是大佬教程为你收集整理的ios – 重用UICollectionView中的单元格时,会短暂显示重用的UICollectionViewCell的旧内容全部内容,希望文章能够帮你解决ios – 重用UICollectionView中的单元格时,会短暂显示重用的UICollectionViewCell的旧内容所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。