
以下是自定义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的旧内容所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)