gpt4 book ai didi

objective-c - ActivityIndi​​cator 不会停止动画,也不会从 UICollectionViewCell 的 super View 中删除

转载 作者:可可西里 更新时间:2023-11-01 06:25:14 30 4
gpt4 key购买 nike

我正在尝试实现 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 %@,activityIndi​​cator);

我得到这个输出:

 activity indicator should be removed <UIActivityIndicatorView: 0xa520ab0; frame = (65 90; 20 20); hidden = YES; layer = <CALayer: 0xa520b60>> 

它显示 UIActivityindicator 已隐藏,但它仍显示在图像顶部

最佳答案

看来您正在重用单元格,所以有不止一个 UIActivityIndi​​catorView

- (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 - ActivityIndi​​cator 不会停止动画,也不会从 UICollectionViewCell 的 super View 中删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12868270/

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