2015-04-18 43 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]; 

我想这应该工作