Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UICollectionView-如何增加要分配的单元格数_Ios_Objective C_Uicollectionviewcell - Fatal编程技术网

Ios UICollectionView-如何增加要分配的单元格数

Ios UICollectionView-如何增加要分配的单元格数,ios,objective-c,uicollectionviewcell,Ios,Objective C,Uicollectionviewcell,我的UICollectionView有一个非零内容插入集 self.collectionView.contentInset = UIEdgeInsetsMake(self.mainNavigation.bounds.size.height, 0, 0, 0); MainNavigation是一个透明的导航栏-一旦用户向下滚动,就可以通过MainNavigation部分查看collectionView。由于collectionView的“屏幕上”帧已增加(这些新单元格未排队),因此初始化了更多单

我的UICollectionView有一个非零内容插入集

self.collectionView.contentInset = UIEdgeInsetsMake(self.mainNavigation.bounds.size.height, 0, 0, 0);
MainNavigation是一个透明的导航栏-一旦用户向下滚动,就可以通过MainNavigation部分查看collectionView。由于collectionView的“屏幕上”帧已增加(这些新单元格未排队),因此初始化了更多单元格

单元格的初始化非常昂贵,并且会导致UI延迟

我需要的是collectionView最初将更多的单元格加载到内存中,以便初始滚动更平滑


如何增加最初加载的单元格数量?

我认为您不需要加载更多单元格,因为这是自然行为。如果你的卷轴不平滑,可能是因为你没有在单元格中正确加载图像

你必须申请签证。例如,您可以这样做(假设您设置了
NSMutableDictionary*imageDic=[[NSMutableDictionary alloc]init];
,您的单元格具有属性
@property(非原子,保留)UIImageView*imageView;
,并且您正在使用)


没有办法做到这一点。

您可以做的是:

  • 优化单元代码并从初始化中移除重部件,可能会延迟一些操作,直到滚动停止

  • 为子视图使用实心背景,并为
    view.opaque
    设置适当的值。它在滚动时提供了良好的性能提升。这是您可以在启用“颜色混合层”选项的情况下在模拟器中调试的内容。您将看到以绿色和红色渲染的命中和未命中

  • 如果您使用自定义Calayer,请尝试在它们是静态的情况下进行栅格化

  • 如果委托在返回单元格之前执行繁重的操作,则缓存数据

  • - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView_ cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    UICollectionViewCell *cell = [collectionView_ dequeueReusableCellWithReuseIdentifier:@"identifier" forIndexPath:indexPath];
    
    NSString* cellKey = [NSString stringWithFormat:@"%d_%d", indexPath.section, indexPath.row];
    NSString* imgName = [cellKey stringByAppendingPathExtension:@"jpg"];
    NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString* imagePath = [documentsDirectory stringByAppendingPathComponent:imgName];
    
    BOOL isDirectory = NO;
    NSFileManager* fm = [NSFileManager defaultManager];
    
    // set image with image in cache (it will be fast!)
    if ([imageDic objectForKey:cellKey])
    {
        UIImage *img = [imageDic objectForKey:cellKey];
        cell.imageView.image = img;
    }
    // set image with image saved in document directory and put it in cache
    else if ([fm fileExistsAtPath:imagePath isDirectory:&isDirectory])
    {
        UIImage *img = [UIImage imageWithContentsOfFile:imagePath];
        cell.imageView.image = img;
        [imageDic setObject:img forKey:cellKey];
    }
    // download image at imageURL, save it and put it in cache too. Until then set image with a placeholder
    else
    {
        __weak UICollectionViewCell* weakCell = cell;
        [cell.imageView setImageWithURLRequest:[NSURLRequest requestWithURL:imageURL] placeholderImage:[UIImage imageNamed:@"placeholder"] success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
            weakCell.imageView.image = image;
            [weakCell setNeedsLayout];
    
            [imageDic setObject:img forKey:cellKey];
            [UIImageJPEGRepresentation(img, 1.f) writeToFile:imagePath atomically:YES];
        } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
            NSLog(@"failed to dl image");
        }];
    }
    
    return cell;
    }