【问题标题】:UICollectionView CoreData. Missplaced cells after updateUICollectionView 核心数据。更新后放错单元格
【发布时间】: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


【解决方案1】:

编辑代码:

    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 确实需要一些额外的技巧。

编辑:

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

}

【讨论】:

  • 感谢您的回复!委托方法报告节中正确的项目数,但单元格仍然随机放置。也许问题在于我以编程方式制作所有内容,包括我的单元格视图。正如您指出的那样,它可能需要更多的工作。你能指出我正确的方向吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-22
  • 2013-12-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多