ios UICollectionViewCell滚动条时间自动重新加载如何停止

ios UICollectionViewCell滚动条时间自动重新加载如何停止,ios,objective-c,uicollectionview,Ios,Objective C,Uicollectionview,在iOS for iPhone应用程序中,我必须制作一个网格,因此我使用的是UICollectionView。问题是当我向下滚动时,它会自动重复数据并占用更多的时间。如何停止数据自动添加,并使数据只加载一次。 这是我的密码 in viewDidLoad : [self.collectionView registerClass:[CVCell class] forCellWithReuseIdentifier:@"cvCell"]; -(void)viewWillAppear:(BOOL)a

在iOS for iPhone应用程序中,我必须制作一个网格,因此我使用的是
UICollectionView
。问题是当我向下滚动时,它会自动重复数据并占用更多的时间。如何停止数据自动添加,并使数据只加载一次。 这是我的密码

in viewDidLoad :
 [self.collectionView registerClass:[CVCell class] forCellWithReuseIdentifier:@"cvCell"]; 

-(void)viewWillAppear:(BOOL)animated {

    [super viewWillAppear:animated];


}


// add Grid

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {

        return [self.setFacebookimage count];
   }

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
   return [setFacebookimage count];


}

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

    // Setup cell identifier
    static NSString *cellIdentifier = @"cvCell";

    CVCell *cell = (CVCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];

    // by nishant set Grid image
    if([[setFacebookimage objectAtIndex:indexPath.row] isEqualToString:@"img19.png" ]){
        cell.cellimageview.image=[UIImage imageNamed:@"img19.png"];
    }else{

        NSData *imageDatafacebookGrid = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: [setFacebookimage objectAtIndex:indexPath.row]]];
        // NSLog(@"%@",imageDatafacebook);
        cell.cellimageview.image=[UIImage imageWithData:imageDatafacebookGrid ];
    }


    return cell;

}
- (CGSize)collectionView:(UICollectionView *)collectionView
                  layout:(UICollectionViewLayout *)collectionViewLayout
  sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    // Adjust cell size for orientation
    if (UIDeviceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation])) {
        return CGSizeMake(100.f, 100.f);
    }
    return CGSizeMake(150.f, 150.f);
}
- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
{
       // NSLog(@"End >>>>> End ");
}

在我的facebook阵列中,我有5个数据。如何解决此问题

您可以在
NSCache
中保存图像。这样它就不会在你每次滚动时刷新你的
单元格
,并且可以提高你的滚动速度。使用
NSCache
可以检查图像是否可用。如果没有,您可以下载并保存在
NSCache

声明
NSCahe*imageCache

然后在
cellForItemAtIndexPath
中尝试以下代码段:

if (cell.cellimageview.image == nil) {
dispatch_queue_t downloadQueue = dispatch_queue_create("image downloader", NULL);
dispatch_async(downloadQueue, ^{
    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString: [setFacebookimage objectAtIndex:indexPath.row]]];
    UIImage *image = [UIImage imageWithData:data];
    [imageCache setObject:image forKey:[setFacebookimage objectAtIndex:indexPath.row]];

    dispatch_async(dispatch_get_main_queue(), ^{
        cell.cellimageview.image = image;
    });
});
}

@尼桑-我的荣幸!!