UICollectionView:不会在重复使用的项目上计算不同大小的项目

我有一个收集视图与我声明的不同项目大小

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { FeedItem *item = [_imagesLinkArray objectAtIndex:indexPath.row]; return CGSizeMake(250, 200*item.sizeFactor); } 

当单元格在collectionView:cellForItemAtIndexPath:被重新使用时collectionView:cellForItemAtIndexPath:正在使用reuesed项目大小呈现项目,而不是在sizeForItemAtIndexPath:指定的sizeForItemAtIndexPath:

有任何想法吗?

看来, collectionView:layout:sizeForItemAtIndexPath:在布局准备,调用CollectionView之前调用一次。

因此,无论何时滚动单元格出列,都将调整为由collectionView:layout:sizeForItemAtIndexPath:提供的大小collectionView:layout:sizeForItemAtIndexPath:处于准备阶段。 你是否期待这个方法被调用,只要有一定的indexPath的单元格可见? 这样你可以dynamic改变单元格大小? 似乎这不是如何使用这种方法,它可以用来在布局准备阶段确定一个单元格的大小。

如果因为某种原因需要重新计算大小,可以使用[UICollectionViewLayout invalidateLayout]使布局[UICollectionViewLayout invalidateLayout]

集合视图似乎重新计算的项目比UITableViewless。 您必须通过调用[collectionView reloadData][collectionView insertItemsAtIndexPaths:indexPaths]来通知关于底层模型的大部分更改的集合视图。

您需要在自定义布局类中 覆盖此函数 如果您使用自定义布局。

 override func shouldInvalidateLayoutForBoundsChange(newBounds: CGRect) -> Bool { return true } 

你可以简单地重写滚动视图委托

 func scrollViewDidScroll(_ scrollView: UIScrollView) { collectionView.collectionViewLayout.invalidateLayout() }