如何更改一个单元格的UICollectionViewLayoutAttributes

我想在不重新加载或更改布局对象的情况下更改一个UICollectionViewCell的UICollectionViewLayoutAttributes属性

我做到了

UICollectionViewLayoutAttributes *attributes = [collectionView layoutAttributesForItemAtIndexPath:indexPath]; attributes.transform = CGAffineTransformMakeScale(1.5, 1.5); 

但这并没有传播到细胞 – 这怎么可能呢?

-[UICollectionView layoutAttributesFor...]方法返回布局属性的副本。 布局返回后,您无法修改原件。

最好的办法是对布局进行子类化,每当您返回该项的属性时(在layoutAttributesForItem或layoutAttributesForElementsInRect中),都返回修改后的属性。

如果更改确实是临时的,您可以随时手动操作单元格转换。 集合视图并不在乎 – 它可能会在需要重新加载单元格时重新应用属性。