UICollectionView核心数据。更新后错位的单元格

时间:2013-10-07 08:15:28

标签: ios core-data uicollectionview

我有一个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;
}

1 个答案:

答案 0 :(得分:0)

编辑代码:

    if (!cell) {
    cell = [[FTRecipeIndexCell alloc] init];
}

删除此代码块。它不是必需的,它只会导致问题

FTRecipeIndexCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];

由于某种原因不会返回细胞。

如果更改任何部分中的内容计数,则UICollectionView必须通过Delegate方法进行更改:

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section

意思是,如果您不在此处动态提供每个部分的记录计数,那么UICollectionView将假定旧计数仍然有效,并将使用您不再需要的旧内容将单元格出列。

如果你正确实现了这个,那么你应该没有问题,因为你最有可能将图像视图作为单元格的子视图,并将它作为属性出口连接。 但是,如果您希望它能够正常有效地工作,那么以编程方式向UICollectionViewCell添加内容确实需要一些额外的技巧。

编辑:

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

上面的代码也不够好:

永远不要添加要查看的内容,而是添加到单元格的contentView 当您以编程方式添加单元格时,您需要更聪明地管理内容,但最后它更清楚,因为您不在代码和NIB之间传播逻辑。我建议你这样做:

- (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)];

// remove subviews, otherwise the content will overlap
for (UIView *current in [cell.contentView subviews]) {[current removeFromSuperview];}
// always add to contentView property
[cell.contentView addSubview:imageView];
return cell;

}