【发布时间】:2021-09-25 00:34:46
【问题描述】:
上下文
我正在使用 NSCollectionLayoutDecorationItem 和 NSCollectionLayoutSection 构建 UICollectionViewLayout。
我做的步骤是:
- 实例化一个NSCollectionLayoutSection,命名为section
- 实例化一个NSCollectionLayoutDecorationItem,命名为decoration
- 将装饰分配给数组 section.decorationItems
- 通过填充 contentInsets 来更新装饰
但是,我在第 4 步中所做的更新并未反映在 section.decorationItems 中。 (交换第 3 步和第 4 步可以解决问题,但仍然不能解释问题。)
问题
NSCollectionLayoutDecorationItem is a class, hence reference type.为什么更改没有反映在数组section.decorationItems中?我不熟悉objective-c,NSCollectionLayoutDecorationItem 是不是类似于objc 中的不可变类?
代码 sn-p
let section = NSCollectionLayoutSection(group: group) // Step 1
let decoration = NSCollectionLayoutDecorationItem.background(elementKind: Self.sectionBackgroundDecorationElementKind) // Step 2
section.decorationItems = [decoration] // Step 3
print("before change \(section.decorationItems)")
decoration.contentInsets = NSDirectionalEdgeInsets(top: 5, leading: 5, bottom: 5, trailing: 5) // Step 4 --> change not reflected in section.decorationItems
print("after change \(section.decorationItems)") // same output as "before change"
【问题讨论】:
-
我不能用我自己的类而不是
NSCollectionLayoutSection来重现这个。你真的可以给minimal reproducible example吗? -
@Sweeper 感谢您的检查。到目前为止,我只在
NSCollectionLayoutSection上发现了这种行为。请参阅this gist 以获取最小的可重现示例。
标签: ios swift objective-c uicollectionviewlayout