Objective c 活动指示器不显示';t在UICollectionViewCell中停止动画或从superview中删除

Objective c 活动指示器不显示';t在UICollectionViewCell中停止动画或从superview中删除,objective-c,ios,uicollectionview,uicollectionviewcell,Objective C,Ios,Uicollectionview,Uicollectionviewcell,我正在尝试实现UICollectionView并显示图像。我使用的是SDWebimage,它在tableviewcells中非常有效,但当我尝试在UICollectionviewCell中使用它时,它不会停止并删除activityindicator。如果没有下载的图像,它会放置占位符图像。我不确定tableviewcell和collectionviewcell之间可能导致此问题的区别 代码如下: -(UICollectionViewCell *)collectionView:(UICollect

我正在尝试实现
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 <UIActivityIndicatorView: 0xa520ab0; frame = (65 90; 20 20); hidden = YES; layer = <CALayer: 0xa520b60>> 
应删除活动指示器

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

Hmm….这很奇怪..您能否尝试确保activityIndicator在主线程上工作-

[cell.ivPersonImage setImageWithURL:[NSURL URLWithString:imgURL] placeholderImage:nil options:SDWebImageProgressiveDownload success:^(UIImage *image, BOOL cached){
        dispatch_async(dispatch_get_main_queue(), ^(void){
            [activityIndicator stopAnimating];
            [activityIndicator removeFromSuperview];
        }
        NSLog(@"activity indicator should be removed");
    }failure:^(NSError *error){
        dispatch_async(dispatch_get_main_queue(), ^(void){
            [activityIndicator stopAnimating];
            [activityIndicator removeFromSuperview];
        }
        cell.ivPersonImage.image = [UIImage imageNamed:@"placeholder.png"];
    }];

我怀疑它不是,这就是为什么它没有停止它的动画。

似乎您正在重用单元格,所以有不止一个
UIActivityIndicatorView
s

- (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;
}

在当前视图控制器的ViewDidLoad中创建活动指示器

UIActivityIndicatorView *spinner = [[UIActivityIndicatorView      alloc]initWithFrame:CGRectMake(150, 225, 20, 30)];
[spinner setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray];
spinner.color = [UIColor blueColor];
[self.view addSubview:spinner];
在导致活动的函数开头使用下面的代码

[NSThread detachNewThreadSelector:@selector(threadStartAnimating:) toTarget:selfwithObject:nil];
声明这两种启动和停止动画的方法

-(void)threadStartAnimating:(id)data
{
[spinner startAnimating];
}


-(void)threadStopAnimating:(id)data
{
[spinner stopAnimating];
}

请给出反馈,因为这段代码在我自己的项目中运行良好

我想知道为什么投票失败?谢谢!所以你看不到下载的图像?或者图像没有下载,这就是为什么活动指示器没有被删除?谢谢你的回复!我看到了图像,并且在一些单元格中活动指示器位于顶部,而不是全部单元格和一些单元格中ls活动指示器冻结尝试放置此行
[cell.ivPersonImageAddSubView:activityIndicator]
setImageWithURL…
@Nekto中设置图像之前…@Nekto感谢您的回复,但这不起作用。奇怪的是,当图像被缓存并放置在单元格中时,活动指示器确实停止,只有在图像下载时才隐藏,然后我发现指示器被隐藏。感谢您的回复。我刚刚能够是的,但仍然是相同的问题。这是uicollectionview实现中的一个问题,因为使用uitableview它可以完美地工作。您也可以通过dispatch\u async将activityIndicator和startAnimating的声明放在主线程中。我遇到了一个类似的问题,activityIndicator不会消失。我通过确保所有UIKit是在主线程中处理的。你在钱上做对了!非常感谢!我永远不会认为这是问题所在。我在做nslog,但在错误的位置。当我将nslog放在返回单元格之前时,它显示了一些带有两个ActivityIndicator的单元格。我唯一要做的事情是在我被删除时投射UIActivityIndicator视图oing[cell.ivPersonImagewithtag:10]奇怪的是,tableview可以完美地工作。我想collectionview有一点不同的机制。再次非常感谢!