【问题标题】:set collection view height after reloadDatareloadData 后设置集合视图高度
【发布时间】:2015-01-21 15:52:11
【问题描述】:

我有一个集合视图,出于某种目的,我想在动态更新单元格后重新调整高度。因此集合视图将显示所有单元格,无需滚动。

我尝试了两种类型的代码,

首先是正常的:

    [self.ProjectCollectionView reloadData];
    [self.ProjectCollectionView layoutIfNeeded];
    NSLog(@"setting contraint to %f from cache", weakself.ProjectCollectionView.contentSize.height);
#endif
    self.ProjectCollectionViewHeightConstraint.constant = self.ProjectCollectionView.contentSize.height;
    [self.ProjectCollectionView layoutIfNeeded];

然而 contentSize 为 0: setting contraint to 0.000000 from cache 来自缓存

我尝试了一个 GCD:

[self.ProjectCollectionView reloadData];
__weak typeof(HomeViewController) *weakself = self;
dispatch_async(dispatch_get_main_queue(), ^{

    NSLog(@"setting contraint to %f from cache", weakself.ProjectCollectionView.contentSize.height);
    weakself.ProjectCollectionViewHeightConstraint.constant = weakself.ProjectCollectionView.contentSize.height;
    [weakself.ProjectCollectionView layoutIfNeeded];
});

这次显示setting contraint to 410.000000 from cache

我一头雾水,第一个代码也应该在主线程上运行,我添加了一个断点并使用线程信息显示:

(lldb) thread info
thread #1: tid = 0x439ee, 0x0000000106c1f411 MCompass`-[HomeViewController fetchUserModelCache](self=0x00007fd258447e50, _cmd=0x0000000106d1dd7b) + 465 at HomeViewController.m:245, queue = 'com.apple.main-thread', stop reason = breakpoint 3.1

为什么不一样?

【问题讨论】:

    标签: ios


    【解决方案1】:

    当您重新加载数据时,请使用以下功能

    - (void)setCollectionViewLayout:(UICollectionViewLayout *)layout animated:(BOOL)animated; // transition from one layout to another
    

    【讨论】:

    • 您能详细说明一下吗?简单地给出一个函数名称并没有多大帮助。 reloadData 和这个函数的顺序是什么?
    • 在集合视图中首先传递新的collectionViewLayout并重新加载数据,它将采用您在setCollectionViewLayout方法中传递的新高度
    • 您能否为此提供一个代码演示并编辑您的答案?谢谢。这将帮助每个人查看您的答案,而不是阅读 cmets
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-15
    • 2014-04-21
    • 2021-10-29
    • 1970-01-01
    • 2020-10-23
    • 1970-01-01
    相关资源
    最近更新 更多