【问题标题】:ActivityIndicator doesn't stop animating nor removes from superview in UICollectionViewCellActivityIndi​​cator 不会停止动画,也不会从 UICollectionViewCell 中的超级视图中删除
【发布时间】:2012-10-12 23:04:39
【问题描述】:

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

【问题讨论】:

  • 我想知道为什么投反对票?谢谢!
  • 所以你没有看到下载的图像?或者图像未下载,这就是未删除活动指示器的原因?
  • 感谢您的回复!我看到图像并在某些单元格中获得活动指示器,而不是全部,在某些单元格中,活动指示器被冻结
  • setImageWithURL...中设置图像之前尝试将此行放在[cell.ivPersonImage addSubview:activityIndicator];
  • @Nekto 感谢您的回复,但没有奏效。奇怪的部分是当图像被缓存并放置在单元格中时,活动指示器确实停止并仅在下载图像时才被隐藏,然后指示器被隐藏。

标签: objective-c ios uicollectionview uicollectionviewcell


【解决方案1】:

您似乎在重复使用单元格,所以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;
}

【讨论】:

  • 你在钱上做对了!非常感谢你!我永远不会认为这是问题所在。我正在做 nslog,但在错误的地方。当我在返回单元格之前放置 nslog 时,它显示了一些带有两个活动指示器的单元格。我唯一要做的就是在我做 [cell.ivPersonImage viewWithTag:10] 时投射 UIActivityIndi​​catorView 奇怪的是,使用 tableview 它可以完美地工作。我猜collectionview有一点不同的机制。再次非常感谢!
【解决方案2】:

嗯....这很奇怪..您可以尝试确保 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"];
    }];

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

【讨论】:

  • 感谢您的回复。我只是能够尝试它,但仍然是同样的问题。这是 uicollectionview 的实现中的东西,因为使用 uitableview 它可以完美地工作。
  • 能不能也把activityIndi​​cator和startAnimating的声明通过dispatch_async放到主线程中。我有一个类似的问题,activityIndi​​cator 不会消失。我通过确保所有 UIKit 在主线程中处理来解决它。
【解决方案3】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-17
    • 1970-01-01
    • 2012-01-30
    • 2018-06-19
    • 1970-01-01
    相关资源
    最近更新 更多