0

こんにちは、iPhoneアプリケーションで画像を表示するためにUICollectionViewを使用していますが、ビューをスクロールすると、読み込まれた画像がなくなり、画像を再度読み込むと、これは「dequeueReusableCellWithReuseIdentifier」が原因であり、これが私のコードです

static NSString * const kCellReuseIdentifier = @"collectionViewCell";
[self.collectionViewPack registerNib:[UINib nibWithNibName:@"CollectionViewItem" bundle:nil] forCellWithReuseIdentifier:kCellReuseIdentifier];

    - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
    {

        UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCellReuseIdentifier forIndexPath:indexPath];

        cell.layer.shouldRasterize = YES;
        cell.layer.rasterizationScale = [UIScreen mainScreen].scale;

        UILabel *titleLabel = (UILabel *)[cell viewWithTag:100];
        UIImageView *icon=(UIImageView *)[cell viewWithTag:101];
        //    [titleLabel setText:[NSString stringWithFormat:@"%d",indexPath.row]];
        [titleLabel setText:[NSString stringWithFormat:@"%@",[arrayImages objectAtIndex:indexPath.row]]];
        icon.image =[UIImage imageNamed:@"loading-1.png"];


        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            NSString *imagePath = [NSString stringWithFormat:@"%@", [test.arrImages objectAtIndex:indexPath.row]];
            NSURL *imageUrl     = [NSURL URLWithString:imagePath];
            NSData *imageData   = [NSData dataWithContentsOfURL:imageUrl];
            UIImage *image      = nil;
            if (imageData){

                image = [[UIImage alloc] initWithData:imageData];
                icon.image = image;

            }

            [image release];

        });



        return cell;


    }

これから私を助けてください。

4

3 に答える 3

3

コードに画像キャッシュを追加する必要があると思います。AFNetworkingを使用することをお勧めします。簡単に実行できます。

import "UIImageView+AFNetworking.h"

……

[cell.imageView setImageWithURL:imageUrl placeholderImage:[UIImage imageNamed:@"default"]];

そして、それは役立つ内部キャッシュを持っています:D

そしてあなたのコードのために...あなたのブロックにこれを追加してみてください

dispatch_async(dispatch_get_main_queue(), ^{
    icon.image = image;
});
于 2013-09-12T14:47:15.350 に答える
0

kCellReuseIdentifierに変更dynamic

static NSString * const kCellReuseIdentifier = @"collectionViewCell";

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView 
                  cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    NSString * kCellReuseIdentifier = 
      [NSString stringWithFormat:@"collectionViewCell%d",indexPath.row];

それはうまくいくかもしれません

于 2013-09-12T13:37:09.540 に答える
0
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView 
                 cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    // Setup cell identifier
    static NSString *cellIdentifier = @"put you viewController here";

    /* this block to use nib-based cells */
     UICollectionViewCell *cell = 
      [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier 
                                                  forIndexPath:indexPath];
    /* end of nib-based cell block */

    /* this block to use subclass-based cells */
  yourviewController *cell = 
  (CVCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier 
                                                      forIndexPath:indexPath];
于 2013-09-12T13:47:37.500 に答える