【发布时间】: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