2015-04-18 120 views
1

我的問題是我得到圖像加載後的活動指標,但它應該出現在圖像加載之前.i'm使用SDWebImageProgressivedownload代碼,但活動指標加載後圖像顯示。請幫助我lazyLoading CollectionView與活動指標

__block UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:activityStyle]; 
activityIndicator.center = imageView.center; 
activityIndicator.hidesWhenStopped = YES; 
[imageView setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"] 
      placeholderImage:[UIImage imageNamed:@"placeholder.png"] 
        success:^(UIImage *image) { [activityIndicator removeFromSuperview]; } 
failure:^(NSError *error) { [activityIndicator removeFromSuperview]; }]; 

[imageView addSubview:activityIndicator]; 
[activityIndicator startAnimating]; 
+0

一些代碼,說明了什麼? – jalone

+0

@jalone謝謝你的回覆。請檢查我更新的代碼 –

回答

1

您遇到的問題是,您正在單元格中添加UIActivityIndi​​catorView,該單元可能會在塊觸發之前得到批處理並重新使用。

爲了解決這個問題,使活動指標的細胞的屬性:

@interface Cell : UICollectionViewCell 

@property (strong, nonatomic) UIActivityIndicatorView activityIndicator; 

@end 

在您的實現

@implementation Cell 

- (id)initWithFrame:(CGRect)frame 
{ 
self = [super initWithFrame:frame]; 
if (self) { 
    [self initialize]; 
} 
return self; 
} 

- (void)awakeFromNib 
{ 
[super awakeFromNib]; 
[self initialize]; 
} 

- (void)initialize 
{ 
// This code is only called once 
self.activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; 
self.activityIndicator.center = self.photoOneImageView.center; 
self.activityIndicator.hidesWhenStopped = YES; 
[self.photoOneImageView addSubview:self.activityIndicator]; 
} 

@end 

然後在您的cellForItemAtIndex方法

[cell.photoOneImageView sd_setImageWithURL:[NSURL URLWithString:@"https://somesite.com/pic.jpg"] 
         placeholderImage:[UIImage imageNamed:@"placeholder.jpg"] 
          completed: 
^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) { 
    [cell.activityIndicator stopAnimating]; 
}]; 

[cell.activityIndicator startAnimating]; 

我想這應該工作