gpt4 book ai didi

ios - 延迟加载带有事件指示器的 CollectionView

转载 作者:行者123 更新时间:2023-11-29 12:24:15 26 4
gpt4 key购买 nike

我的问题是我在图像加载后获得事件指示器,但它应该在图像加载之前出现。我正在使用 SDWebImageProgressivedownload 代码,但事件指示器在图像显示后加载。请帮助我

__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];

最佳答案

您遇到的问题是,您在单元格上添加了一个 UIActivityIndi​​catorView,该单元格可能会在您的 block 触发之前被取消队列并在稍后重用。

要解决此问题,请将事件指示器设置为您的单元格的属性:

@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];

应该可以吧

关于ios - 延迟加载带有事件指示器的 CollectionView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29717460/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com