ActivityIndi​​cator不会停止在UICollectionViewCell中设置动画或从superview中删除

时间:2012-10-12 23:04:40

标签: objective-c ios uicollectionview uicollectionviewcell

我正在尝试实施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已隐藏,但仍显示在图片顶部

3 个答案:

答案 0 :(得分:7)

您似乎正在重复使用单元格,因此有多个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;
}

答案 1 :(得分:1)

嗯....这很奇怪..你可以尝试确保activityIndi​​cator正在主线程上工作 -

[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"];
    }];

我怀疑它不是,这就是它没有停止动画的原因。

答案 2 :(得分:0)

在当前View Controller的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];
}

请提供反馈,因为此代码在我自己的项目中正常工作