【发布时间】:2012-10-12 23:04:39
【问题描述】:
我正在尝试实现UICollectionView 并显示图像。我正在使用SDWebimage,它在tableviewcells 中完美运行,但是当我尝试在UICollectionviewCell 中使用它时,它不会停止并删除activityindicator。如果没有下载的图像,它会放置占位符图像。我不确定可能导致此问题的 tableviewcell 和 collectionviewcell 之间有什么区别。
代码如下:
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"personImageCell";
PersonCollectionViewCell *cell = (PersonCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
Person *person = [self.fetchedResultsController objectAtIndexPath:indexPath];
NSString *imgURL=[person.imageurl stringByAppendingString:@"?maxheight=300&maxwidth=400"];
UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
activityIndicator.hidesWhenStopped = YES;
activityIndicator.hidden = NO;
[activityIndicator startAnimating];
activityIndicator.center = CGPointMake(cell.ivPersonImage.frame.size.width /2, cell.ivPersonImage.frame.size.height/2);
[cell.ivPersonImage setImageWithURL:[NSURL URLWithString:imgURL] placeholderImage:nil options:SDWebImageProgressiveDownload success:^(UIImage *image, BOOL cached){
[activityIndicator stopAnimating];[activityIndicator removeFromSuperview];
NSLog(@"activity indicator should be removed");
}failure:^(NSError *error){
[activityIndicator stopAnimating];[activityIndicator removeFromSuperview];
cell.ivPersonImage.image = [UIImage imageNamed:@"placeholder.png"];
}];
[cell.ivPersonImage addSubview:activityIndicator];
return cell;
}
更新:
当我做NSLog(@"activity indicator should be removed %@,activityIndicator);
我得到这个输出:
activity indicator should be removed <UIActivityIndicatorView: 0xa520ab0; frame = (65 90; 20 20); hidden = YES; layer = <CALayer: 0xa520b60>>
它显示UIActivityindicator 已隐藏,但仍显示在图像顶部
【问题讨论】:
-
我想知道为什么投反对票?谢谢!
-
所以你没有看到下载的图像?或者图像未下载,这就是未删除活动指示器的原因?
-
感谢您的回复!我看到图像并在某些单元格中获得活动指示器,而不是全部,在某些单元格中,活动指示器被冻结
-
在
setImageWithURL...中设置图像之前尝试将此行放在[cell.ivPersonImage addSubview:activityIndicator]; -
@Nekto 感谢您的回复,但没有奏效。奇怪的部分是当图像被缓存并放置在单元格中时,活动指示器确实停止并仅在下载图像时才被隐藏,然后指示器被隐藏。
标签: objective-c ios uicollectionview uicollectionviewcell