UITableViewCell 调整大小不流畅
UITableViewCell Resizing is not smooth
我正在尝试根据滑块的值更改 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;
}
我认为您不需要重新加载所有单元格。仅重新加载屏幕上可见或当前活动的单元格。试试这个,如果它的工作。
NSArray *visibleCellIndexPaths = [collectionView indexPathsForVisibleItems];
[collectionView reloadItemsAtIndexPaths:visibleCellIndexPaths]
正如问题评论中所确认的,加载图像是问题的原因。
先分配一个图片数组,如果products.count
相对小可以解决问题。或者您将需要某种 'smarter' 缓存服务 运行 在后台提供屏幕上所需的正确图像,因为我看到您也在使用过滤器。
在单独的线程中加载图像在这两种情况下都应该有所帮助,这样您就可以确定是从数据还是从现有的缓存版本(可能是数组或其他类型)加载额外的文件,并丢弃未使用的图像,如果您以后可以修改单元格。
我正在尝试根据滑块的值更改 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;
}
我认为您不需要重新加载所有单元格。仅重新加载屏幕上可见或当前活动的单元格。试试这个,如果它的工作。
NSArray *visibleCellIndexPaths = [collectionView indexPathsForVisibleItems];
[collectionView reloadItemsAtIndexPaths:visibleCellIndexPaths]
正如问题评论中所确认的,加载图像是问题的原因。
先分配一个图片数组,如果products.count
相对小可以解决问题。或者您将需要某种 'smarter' 缓存服务 运行 在后台提供屏幕上所需的正确图像,因为我看到您也在使用过滤器。
在单独的线程中加载图像在这两种情况下都应该有所帮助,这样您就可以确定是从数据还是从现有的缓存版本(可能是数组或其他类型)加载额外的文件,并丢弃未使用的图像,如果您以后可以修改单元格。