【问题标题】:UICollectionView - How to increase the number of cells to be allocatedUICollectionView - 如何增加要分配的单元格数量
【发布时间】:2014-12-03 10:40:18
【问题描述】:

我的 UICollectionView 有一个非零的 contentInset

self.collectionView.contentInset = UIEdgeInsetsMake(self.mainNavigation.bounds.size.height, 0, 0, 0);

MainNavigation 是一个透明的导航栏——一旦用户向下滚动,可以通过 MainNavigation 部分看到 collectionView。更多单元格被初始化,因为 collectionView 的“屏幕上”框架增加了(这些新单元格没有出队)。

单元格的初始化非常昂贵,并导致 UI 滞后。

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

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

【问题讨论】:

    标签: ios objective-c uicollectionviewcell


    【解决方案1】:

    我认为您不需要加载更多单元格,因为这是自然行为。如果您的滚动不流畅,可能是因为您没有将图像正确加载到单元格中。

    您必须申请lazy loading pattern。例如,您可以这样做(假设您设置了NSMutableDictionary* imageDic = [[NSMutableDictionary alloc] init];,您的单元格具有属性@property(nonatomic, retain) UIImageView *imageView;,并且您正在使用AFNetworking

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

    【讨论】:

      【解决方案2】:

      没有办法做到这一点。

      你可以做的是:

      1. 优化单元格代码并从初始化中删除繁重的部分,可能会延迟某些操作直到滚动停止。

      2. 为子视图使用纯色背景并为view.opaque 设置适当的值。它在滚动时提供了良好的性能提升。这是您可以在模拟器中调试“颜色混合图层”选项打开的东西。您会看到以绿色和红色呈现的命中和未命中。

      3. 如果您使用自定义 CALayers,请尝试在它们是静态时/如果它们是静态的。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-08-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多