gpt4 book ai didi

ios - 如何在 Swift 中手动更改 UICollectionView 高度

转载 作者:行者123 更新时间:2023-11-28 09:08:51 24 4
gpt4 key购买 nike

我隐藏了在 collectionView 中完成的行。需要时,我在 cellForItemAtIndexPath 中调用 cell.hidden = isCellHidden

在我隐藏 10 行后,剩下很多空白空间,我想缩小 collectionView 的大小以仅适合未隐藏的行。

collectionView 的设计有点像tableView。我知道使用 tableView 我必须做的就是设置:

func section1VisibilityButton(sender: UIButton){
isCellHidden = !isCellHidden
self.tableView.reloadData()
self.tableView.contentSize.height = CGFloat(500)
}

当我尝试使用 collectionView 时,它会正确调整大小,但一旦我尝试向下滚动,它就会将自身调整回原始高度,包括隐藏的单元格(单元格层仍然隐藏,但下面有大量空白空间最后可见的行就好像它们是可见的一样)

最佳答案

对于您的问题,有两个选项可以更改您的 collectionView/tableView 的框架。

  1. 如果你正在使用autolayout,你需要创建IBOutlet of bottom constraintIBOutlet of constant height constraint你的tableView (您正在使用的这些约束中的任何一个)。

  2. reload tableView 之后您需要通过计算 height更新约束 的数据.

假设您正在使用 constant height constraint和你计算出的height150 (例如,3 行,每行 50 高度)。

constraintTableViewHeight.constant = 150;//this will change height

self.view.layoutIfneed(); // this will apply updated constraints to whole view
  1. 如果您不使用自动布局,您可以通过更改 tableView.frame 属性手动更改高度。

关于ios - 如何在 Swift 中手动更改 UICollectionView 高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30066771/

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