简体   繁体   English

滚动后 CollectionView 单元格更改大小 - AutoLayout

[英]CollectionView Cell changing size after scrolling - AutoLayout

I have a Collection View nested in a Collection View.我有一个嵌套在集合视图中的集合视图。 What I have done to try and have the cell size change dynamically based on content is create an outlet for the height of the child collection and change its constant after getting the content size.我所做的尝试根据内容动态更改单元格大小是为子集合的高度创建一个出口,并在获取内容大小后更改其常量。

This is a screen show of my IB constraints, and I'm using the height one highlighted.这是我的 IB 约束的屏幕显示,我正在使用突出显示的高度。

在此处输入图片说明

So i have the child collection view (aqua's color box) constrained to the cell of the parent on 4 sides with height constant.因此,我将子集合视图(aqua 的颜色框)限制在高度恒定的 4 边上的父单元格中。

In my custom cell class, which holds the child collection view, I am setting the height like:在包含子集合视图的自定义单元格类中,我将高度设置为:

    let contentHeight = innerCollectionView.collectionViewLayout.collectionViewContentSize.height
        innerCollectionHeight.constant = contentHeight

And then in my view controller class which holds the parent collection view in viewDidLoad i specify the estimated item size with:然后在我在 viewDidLoad 中保存父集合视图的视图控制器类中,我指定了估计的项目大小:

    let flowLayout = outerCollectionView.collectionViewLayout as! UICollectionViewFlowLayout
        flowLayout.estimatedItemSize = CGSize(width: view.frame.width - 20, height: view.frame.height / 4)
        flowLayout.minimumLineSpacing = 10
        flowLayout.sectionInset = UIEdgeInsets(top: 0, left: 10, bottom: 10, right: 10)

The result is that on the initial load everything displays correctly, however after scrolling the top cell which has 2 rows of data (green number icons) adjusts itself to be the same as the others.结果是在初始加载时一切都显示正确,但是在滚动具有 2 行数据(绿色数字图标)的顶部单元格后,将自身调整为与其他单元格相同。 You can see in the gif below that the child collection view (the aqua'ish coloured box) is still the same size, it is the parent cell (orange box) that has reduced in size.您可以在下面的 gif 中看到子集合视图(浅绿色框)的大小仍然相同,是父单元格(橙色框)的大小减小了。

在此处输入图片说明

I'm stumped here as to what to do.我不知道该怎么做。

the bit where it goes back to full size is just the gif reseting it doesn't actually do that.它恢复到全尺寸的那一点只是重置它的 gif,它实际上并没有这样做。

inside cellForItemAt (where outerCell is the normally declared cell) redeclare delegate and then reload the child collection:在 cellForItemAt 内(其中,outerCell 是通常声明的单元格)重新声明委托,然后重新加载子集合:

outerCell.setCollectionViewDataSourceDelegate(dataSourceDelegate: self, forItem: indexPath.item)
outerCell.innerCollectionView.reloadData()
outerCell.innerCollectionView.layoutIfNeeded()

Try setting in storyboard Estimate Size for your collection view to none .尝试在 Storyboard Estimate Size中将您的收藏视图设置为none

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM