具有可变单元格大小的 UICollectionView [英] UICollectionView with variable cell sizes

查看:19
本文介绍了具有可变单元格大小的 UICollectionView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 UICollectionView 中的单元格大小方面遇到了一些困难.在android中,您可以轻松地包装"单元格的大小.就像在 iOS 中一样,您有一个函数调用GetCell",您可以决定它的大小.

I'm struggling a little bit with the size for cells in UICollectionView. In android, you can easily "wrap" the size of the cell. Just like in iOS, you have a function call 'GetCell' and you decide how big it will be.

iOS 的不同之处在于,在(UICollectionViewController 的)getCell"函数中,您似乎无法选择单元格(或内容视图)的大小.如果我更改大小,它会忽略它并使用 CollectionView 的常规ItemSize"(对于所有单元格都相同).

The difference in iOS is that in the "getCell" function (of UICollectionViewController) it seems you can't choose the size of the cell (or the contentview). If I change the size, it will ignore it and use anyway the general 'ItemSize' of the CollectionView (which is the same for all cells).

这有时会导致视图不是很漂亮.例如,如果我有一个包含图像的水平列表,我希望图像之间的距离相同,如果一个图像是 200x200 而另一个是 400x200,则独立.所以单元格大小也应该不同.

This sometimes results in Views which are not very beautiful. For example, if I have a horizontal list with images, I want the distance between images to be the same, independent if one image is 200x200 and the other 400x200. So the cell size should be different also.

可以为不同的单元格定义不同的大小.您可以使用 Collectionview 委托和 GetSizeForItem(= ObjC 中的 sizeForItemAtIndexPath)函数.问题是,这个函数在实际的 GetCell 函数之前被调用.

It is possible to define a different size for different cells. You can use the Collectionview delegate and the GetSizeForItem (= sizeForItemAtIndexPath in ObjC) function. The problem is, this function is called BEFORE the actual GetCell function.

所以如果我有一个更复杂的单元格,例如一些标签.在我的GetCell"函数中,我构建了这个 Cell,最后,当返回 Cell 时,我知道它应该的大小.但是,在 GetSizeForItem 函数中,该信息尚不可用,因为 Cell 仍为空".

So if I have a more complex Cell, with for example some labels. In my "GetCell" function, I build this Cell and at the end, when returning the Cell, I know which size it should be. However, in the GetSizeForItem function, that info is not available yet, because the Cell is still 'null'.

我能做到的唯一方法是在调用 'GetSizeForItem' 时为单元实际构建 UIView(以便我可以请求大小).但这似乎不是一个好的设计,因为我在GetCell"之前构建了 UIView,我将在那里再次构建它.

The only way I could do it, is to actually build the UIView for the cell (so I can request the size) at the moment of the 'GetSizeForItem' call. But this doesn't seem a good design, because I'm building the UIView before the 'GetCell' where I will build it again.

有什么我忽略的东西吗?

Is there something I'm overlooking?

问候,马特

推荐答案

确实 GetSizeForItem 与 GetCell 分开调用.这样做是因为创建 UIViews 是一项非常耗时和消耗内存的任务,您的应用程序要么内存不足,要么必须处理其他视图才能处理大列表.

Indeed GetSizeForItem gets called separately from GetCell. It's done that way because creating UIViews is a very time and memory consuming task, and your application would either run out of memory or have to dispose other views to be able to handle big lists.

在视图呈现之前,UICollectionView(和 UITableView)会询问列表中所有(或大部分)元素的大小和位置,因此它可以知道在哪里绘制它们.但是,其中许多元素将不可见,因此 collectionView 避免了必须创建它们.这就是为什么 GetSizeForItem 被预先调用,而 GetCell 只在稍后调用的原因.

Before the view gets presented, the UICollectionView (and UITableView) asks for the sizes and positions of all (or most) elements in the list, so it can know where to draw them. Many of those elements won't be visible though, so the collectionView avoids having to create them. This is why the GetSizeForItem gets called upfront, and the GetCell only later.

在您的情况下,尝试将计算视图大小的逻辑与视图本身分开.使它成为一个不需要视图存在的简单数学公式,因此它足够快,可以预先运行.

In your case, try to separate the logic that calculates the size of the view from the view itself. Make it a simple math formula that doesn't require a view to exist, so it's fast enough to be run upfront.

这篇关于具有可变单元格大小的 UICollectionView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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