我试图根据滑块的值更改UICollectionViewCell大小。现在,我通过每次更改滑块值时在reloadData上调用UICollectionView来管理此问题。问题在于,对于大数据源,刷新不顺畅,有时会释放应用程序一段时间。有什么办法可以增强这个效果吗?我指定我的单元格中有图像。这是我写的代码:

- (IBAction)didChangeCellSize:(UISlider *)sender
{
    [self.collectionView reloadData];
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    float size = 120.0 * (self.cellSizeSlider.value + 1);

    return CGSizeMake(size, size);
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ProductCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"productCollectionViewCell" forIndexPath:indexPath];

    if ((self.filteredProducts == nil && self.products.count > 0  && indexPath.row < self.products.count) || (self.filteredProducts && self.filteredProducts.count > 0  && indexPath.row < self.filteredProducts.count))
    {
        NSDictionary            *product;
        NSData                  *imageData;

        if (self.filteredProducts)
        {
            product = [self.filteredProducts objectAtIndex:indexPath.row];
        }
        else
        {
            product = [self.products objectAtIndex:indexPath.row];
        }
        imageData = product[ParseDataManagerItemImageData];
        if (imageData)
        {
            UIImage             *image = [UIImage imageWithData:imageData];

            if (image)
            {
                cell.productImageView.image = image;
            }
            else
            {
                cell.productImageView.image = [UIImage imageNamed:@"DefaultCartItem"];
            }
        }
        else
        {
            cell.productImageView.image = [UIImage imageNamed:@"DefaultCartItem"];
        }
        if (self.editMode)
        {
            cell.deleteButton.hidden = NO;
        }
        else
        {
            cell.deleteButton.hidden = YES;
        }
        cell.productNameLabel.text = [product[DataManagerItemTitle] isKindOfClass:[NSString class]] ? product[DataManagerItemTitle] : @"";
        cell.indexPath = indexPath;
        cell.productsVC = self;
    }
    return cell;
}

最佳答案

如问题注释所确认,加载图像是问题的原因。

如果products.count相对较小,则首先分配图像阵列可以解决该问题。否则,您将需要在后台运行某种“更智能”的缓存服务以提供屏幕上所需的正确图像,因为我看到您也在使用过滤器。

在两种情况下,将图像加载到单独的线程中都应该有所帮助,以便您可以确定是否从数据或从现有的缓存版本(可能是数组或其他类型)中加载额外的文件,如果以后可以修改单元格,则丢弃未使用的图像。 。

关于ios - UITableViewCell调整大小不流畅,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34589716/

10-14 23:45
查看更多