gpt4 book ai didi

objective-c - 设置数据后调整 UICollectionView 单元格的大小

转载 作者:IT老高 更新时间:2023-10-28 11:36:22 27 4
gpt4 key购买 nike

我的 UICollectionView 单元格包含带有多行文本的 UILabel。在标签上设置文本之前,我不知道单元格的高度。

-(CGSize)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout*)collectionViewLayout
sizeForItemAtIndexPath:(NSIndexPath *)indexPath;

这是我研究单元格大小的初始方法。但是,这在从 Storyboard 中创建单元之前调用。

有没有办法在渲染后对集合进行布局和调整单元格的大小,并且我知道单元格的实际大小?

最佳答案

我认为您正在寻找可以在 UICollectionView 的 .collectionViewLayout 属性上调用的 invalidateLayout 方法。此方法会重新生成您的布局,在您的情况下,这意味着还调用 -collectionView: layout: sizeForItemAtIndexPath:,这是反射(reflect)所需项目大小的正确位置。 Jirune 指出了如何计算它们的正确方向。

invalidateLayout 的使用示例可见here .另请参阅有关该方法的 UICollectionViewLayout 文档:

Invalidates the current layout and triggers a layout update.

Discussion:

You can call this method at any time to update the layout information. This method invalidates the layout of the collection view itself and returns right away. Thus, you can call this method multiple times from the same block of code without triggering multiple layout updates. The actual layout update occurs during the next view layout update cycle.

编辑:

对于包含自动布局约束的storyboard collection view,你需要重写UIViewControllerviewDidLayoutSubviews方法,并在此调用invalidateLayout collection view layout方法。

- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
[yourCollectionView.collectionViewLayout invalidateLayout];
}

关于objective-c - 设置数据后调整 UICollectionView 单元格的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13750428/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com