【问题标题】:UITableViewCell Resizing is not smoothUITableViewCell 调整大小不流畅
【发布时间】:2016-01-04 11:06:30
【问题描述】:

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

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

【问题讨论】:

  • 如果图像是问题(删除它们并测试响应时间),尝试在后台加载图像而不是?
  • 顺便说一句,我想你可以在调整大小时使用setCollectionViewLayout
  • 图像绝对是问题所在。从数据中重新分配图像的每个单元需要很长时间。所以我现在将所有图像分配到一个数组中,并且只在cellForItemAtIndexPath 中分配它。如果你想写答案,我会接受。 :)

标签: ios objective-c resize uicollectionview


【解决方案1】:

正如问题评论中所确认的,加载图像是问题的原因。

先分配一个图像数组,如果products.count相对小,可以解决问题。或者你需要在后台运行某种“更智能”的缓存服务来提供屏幕所需的正确图像,因为我看到你也在使用过滤器。

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

【讨论】:

    【解决方案2】:

    我认为您不需要重新加载所有单元格。仅重新加载屏幕上可见或当前活动的单元格。试试这个,如果它的工作。

    NSArray *visibleCellIndexPaths = [collectionView indexPathsForVisibleItems];
    [collectionView reloadItemsAtIndexPaths:visibleCellIndexPaths]
    

    【讨论】:

    • 这并不能解决问题。还是谢谢。
    猜你喜欢
    • 2021-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-02
    • 2023-03-27
    • 2014-01-10
    • 1970-01-01
    相关资源
    最近更新 更多