滚动后CollectionView单元格更改大小-AutoLayout [英] CollectionView Cell changing size after scrolling - AutoLayout

查看:64
本文介绍了滚动后CollectionView单元格更改大小-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.

这是我的IB约束的屏幕显示,我使用的是突出显示的高度.

This is a screen show of my IB constraints, and I'm using the height one highlighted.

因此,我将子集合视图(浅绿色的颜色框)限制在高度恒定的4个边上的父单元格中.

So i have the child collection view (aqua's color box) constrained to the cell of the parent on 4 sides with height constant.

在保存子集合视图的自定义单元格类中,我将高度设置为:

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

然后在将父集合视图保存在viewDidLoad中的视图控制器类中,我使用以下参数指定估计的项目大小:

And then in my view controller class which holds the parent collection view in viewDidLoad i specify the estimated item size with:

    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)

结果是,在初始加载时,所有内容均可正确显示,但是在滚动具有两行数据(绿色数字图标)的顶部单元格后,其自身将进行调整,使其与其他单元格相同.您可以在下面的gif图片中看到子集合视图(浅绿色的框)的尺寸仍然相同,是父单元格(橙色框)的尺寸减小了.

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. 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重置,实际上并没有做到这一点.

the bit where it goes back to full size is just the gif reseting it doesn't actually do that.

推荐答案

在cellForItemAt内部(其中,cell是通常声明的单元格)重新声明委托,然后重新加载子集合:

inside cellForItemAt (where outerCell is the normally declared cell) redeclare delegate and then reload the child collection:

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

这篇关于滚动后CollectionView单元格更改大小-AutoLayout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆