【发布时间】:2013-10-07 08:15:27
【问题描述】:
我有一个collectionView,其中一个部分显示图像网格。 问题是当我进行更新并且结果计数小于初始项目计数时。 单元格随机放置在“旧”空白点,但我希望 collectionView 从左上角开始一个接一个地放置它们。
我在进行更新时使用此代码:
// Fetch request above this code
[_collectionView performBatchUpdates:^{
[_collectionView reloadSections:[NSIndexSet indexSetWithIndex:0]];
} completion:nil];
[_collectionView reloadData];
我没有自定义 flowLayout。我正在使用这个:
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.minimumInteritemSpacing = 0;
layout.minimumLineSpacing = 0;
我应该删除动画块中的单元格吗?
有没有办法强制collectionView完全重新加载?
编辑:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
FTRecipeIndexCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
if (!cell) {
cell = [[FTRecipeIndexCell alloc] init];
}
Recipe *recipe = [fetchedResultsController objectAtIndexPath:indexPath];
cell.imageView.image = [UIImage imageWithData:recipe.thumbnail];
float size = _collectionView.bounds.size.width / 6;
[cell setFrame:CGRectMake(0, 0, size, size)];
return cell;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return [fetchedResultsController.fetchedObjects count];
}
让它工作。 我错误地设置了 cellFrame 导致单元格的狂野西部定位。 因为我只在单元格上使用 imageView,所以我用这段代码替换了我的自定义单元格。
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
Recipe *recipe = [fetchedResultsController objectAtIndexPath:indexPath];
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageWithData:recipe.thumbnail]];
float size = _collectionView.bounds.size.width / 6;
[imageView setFrame:CGRectMake(0, 0, size, size)];
[cell addSubview:imageView];
return cell;
}
【问题讨论】:
-
删除单元格会非常低效。方法:dequeueReusableCellWithReuseIdentifier:forIndexPath 非常有效地完成了这项工作,删除单元格是不明智的。虽然我想看更多代码,但我怀疑问题出在 - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 你能告诉我们它的代码吗?
-
请编辑您的原始问题并添加代码。
标签: ios core-data uicollectionview