作者热门文章
- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在尝试实现 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
已隐藏,但它仍显示在图像顶部
最佳答案
看来您正在重用单元格,所以有不止一个 UIActivityIndicatorView
。
- (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 = [cell.ivPersonImage viewWithTag:10];
if (activityIndicator) [activityIndicator removeFromSuperview];
activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
activityIndicator.hidesWhenStopped = YES;
activityIndicator.hidden = NO;
[activityIndicator startAnimating];
activityIndicator.center = cell.ivPersonImage.center;
activityIndicator.tag = 10;
[cell.ivPersonImage addSubview:activityIndicator];
[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"];
}];
return cell;
}
关于objective-c - ActivityIndicator 不会停止动画,也不会从 UICollectionViewCell 的 super View 中删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12868270/
我是一名优秀的程序员,十分优秀!