优化非常大的collectionview

时间:2016-09-19 22:14:55

标签: ios objective-c optimization uicollectionviewcell sdwebimage

我有一个非常大的集合视图(它会不断变大)并且问题是,每次集合视图变大时,数据呈现得更慢并且滞后。是否有任何好的技巧来优化这个?现在,用于定义每个单元格中显示内容的所有逻辑都是从一个名为" totsalesOrders"的数组中获取的,其中包含一个名为BUYProduct的自定义对象。这些被提取并添加到数组中每50个项目。

以下是indexpath的单元格的代码:

BUYProduct * product = [totsalesOrder objectAtIndex:indexPath.row];
    //View for trending feed
    CHTCollectionViewWaterfallCell *cell =
    (CHTCollectionViewWaterfallCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"cell"
                                                                                forIndexPath:indexPath];

    BUYImageLink * imgLink = [[product imagesArray] firstObject];
    [cell.imageView sd_setImageWithURL:imgLink.sourceURL
                      placeholderImage:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                          NSNumber * width = [NSNumber numberWithFloat:image.size.width];
                          [imageSizesW setValue:width forKey:[NSNumber numberWithInteger:indexPath.row]];
                          NSNumber * height = [NSNumber numberWithFloat:image.size.height];
                          [imageSizesH setValue:height forKey:[NSNumber numberWithInteger:indexPath.row]];
                          [self.collectionView.collectionViewLayout invalidateLayout];

    }];
    cell.productDict = product;
    cell.layer.borderColor = [UIColor lightGrayColor].CGColor;
    cell.layer.borderWidth = 0.2f;

    return cell;

不幸的是,我们必须对每个单元格使用某种优化,因此动态计算单元格大小,这可以通过以下方式完成:

        CGSize siz;
    if ([imageSizesW objectForKey:[NSNumber numberWithInteger:indexPath.row]] != nil) {
        NSNumber * imgWi = [imageSizesW objectForKey:[NSNumber numberWithInteger:indexPath.row]];
        NSNumber * imgHe = [imageSizesH objectForKey:[NSNumber numberWithInteger:indexPath.row]];
        //NSLog(@"Numbers:%@, %@", imgWi, imgHe);
        CHTCollectionViewWaterfallCell * cell = (CHTCollectionViewWaterfallCell*)[collectionView cellForItemAtIndexPath:indexPath];
        NSLog(@"product name:%@    and it's collection: %lu", cell.productDict.title,(unsigned long)cell.productDict.collections.count);
        float halfwi = collectionView.frame.size.width/2-15;
        float factor = halfwi/([imgWi floatValue]);
        float height = ([imgHe intValue]) * factor;
        //float ediHeight = (roundf(height/10.0))*10.0;
        //NSLog(@"FLOATS %f, %f %f",halfwi, ediHeight, factor);
        if (([imgHe floatValue]-[imgWi floatValue])<10&&([imgHe floatValue]-[imgWi floatValue])>-10) {
            siz = CGSizeMake(halfwi, height);
        }else{
            siz = CGSizeMake(halfwi, height*1.5);
        }
    }
    else {
        siz = CGSizeMake(100, 100);
    }
    return siz;

当我分析应用程序耗尽的内存数量时几乎没有任何内容,但是当收集视图中的单元格数量增加时,我们仍有一些延迟。

感谢, 马丁

0 个答案:

没有答案