3

画像を実装UICollectionViewして表示しようとしています。テーブルビューセルで完全に機能するものを使用していSDWebimageますが、使用しようとすると、アクティビティインジケーターが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

セルを再利用しているようですので、複数ありますUIActivityIndicatorView

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

アニメーションを開始および停止するためのこれらの 2 つのメソッドを宣言します。

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


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

このコードは自分のプロジェクトで正常に動作しているため、フィードバックをお寄せください

于 2014-01-22T13:52:02.253 に答える