我有一个 UICollectionView,其中 UIActivityIndicator 在加载图像之前显示在单元格中,然后无论 UIImageView+AFNetworking 块中的成功还是失败都会将其关闭。但是,有时我会得到似乎没有隐藏或停止动画的剩余活动指示器。
这是我的 cellForItemAtIndexPath 的一部分:
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath {
...
NSURL *imageURL = [NSURL URLWithString:[tmpcamera objectForKey:@"url"]];
NSURLRequest *imageRequest = [NSURLRequest requestWithURL:imageURL cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:10];
UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
activityIndicator.hidesWhenStopped = YES;
activityIndicator.hidden = NO;
activityIndicator.frame = CGRectMake(0, 0, 130, 110);
activityIndicator.tag = 10;
[cell.imageView addSubview:activityIndicator];
[activityIndicator startAnimating];
__weak typeof(FavouriteCell) *weakImage = cell;
[cell.imageView setImageWithURLRequest:imageRequest
placeholderImage:[UIImage imageNamed:@"trans50.png"]
success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *i)
{
[activityIndicator stopAnimating];
activityIndicator.hidden = YES;
[activityIndicator removeFromSuperview];
[weakImage.imageView setImage:i];
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error)
{
[activityIndicator stopAnimating];
activityIndicator.hidden = YES;
[activityIndicator removeFromSuperview];
[weakImage.imageView setImage:[UIImage imageNamed:@"errorimage.png"]];
}];
...
}
我不确定这段代码是否足以调试问题所在,但任何想法都会受到赞赏。
我正在谈论的示例屏幕截图如下: