2013-04-29 5 views
6

में कोशिकाओं का पुन: उपयोग करते समय मैं UICollectionView का उपयोग कर रहा हूं ताकि चित्रों की ग्रिड प्रदर्शित हो सके जो यूआरएल से असीमित रूप से लोड हो। मेरा संग्रह दृश्य UICollectionViewCells प्रदर्शित करने के लिए पुन: प्रयोज्य कोशिकाओं का उपयोग करता है। कोशिकाओं का पुन: उपयोग नहीं होने पर सब कुछ ठीक से प्रदर्शित होता है, लेकिन जब मैं थोड़ा सा स्क्रॉल करता हूं, तब पुन: उपयोग किए गए कोशिकाएं अपने सामान्य व्यवहार शुरू करने से पहले पुरानी सामग्री को संक्षिप्त रूप से फ्लैश करती हैं।एक पुन: उपयोग की गई UICollectionViewCell की पुरानी सामग्री को संक्षेप में प्रदर्शित किया जाता है जब UICollectionView

#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 के कार्यान्वयन है:

यहां कस्टम UICollectionViewController का कार्यान्वयन है।

#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 

उत्तर

5

सेल समाप्त होने पर सेल को शून्य के लिए यूआरएल सेट करने का प्रयास कर सकते हैं, यह मेरी परियोजना पर काम करता है।

-(void)collectionView:(UICollectionView *)cv didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [(MyCollectionViewCell *)cell setUrlString:nil]; 
} 

मुझे उम्मीद है कि इससे मदद मिलती है!

18

आप अपने UICollectionViewCell सबक्लास में -(void)prepareForReuse लागू कर सकते हैं। और वहां अपनी रीसेटिंग करें।

+0

इस स्वीकार्य उत्तर से इस के पास अधिक वोट क्यों हैं। और क्या दो उत्तरों के बीच कोई अंतर है? –

+0

ऐप्पल चाहता है कि ऐप्पल आपको पुन: उपयोग करने के लिए तैयार करने के लिए सेल को रीसेट करते समय उपयोग करना चाहता है। – Weston

1

This आलेख @Weston और @architectpianist समाधानों के बीच अंतर दिखाता है।

संक्षेप में: चूंकि आईओएस 10 -(void)prepareForReuse अधिक इष्टतम है।

संबंधित मुद्दे