3

我正在尝试实现UICollectionView和显示图像。我正在使用SDWebimage它在 tableviewcells 中完美运行,但是当我尝试在其中使用它时,UICollectionviewCell它不会停止并删除活动指示器。如果没有下载的图像,它会放置占位符图像。我不确定可能导致此问题的 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是隐藏的,但它仍然显示在图像的顶部

4

3 回答 3

7

看来您正在重用单元格,所以有多个UIActivityIndicatorViews。

- (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;
}
于 2012-10-17T05:58:43.817 回答
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"];
    }];

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

于 2012-10-15T01:52:30.510 回答
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];
}

请提供反馈,因为此代码在我自己的项目中运行良好

于 2014-01-22T13:52:02.253 回答