更快地估算CollectionView中的单元格高度 [英] Faster way to estimate cell height in a CollectionView

查看:449
本文介绍了更快地估算CollectionView中的单元格高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的UICollectionView中有一个无限滚动,我注意到我估计单元格高度的方式是我的集合视图的瓶颈。它导致一些长时间的延迟,我滚动我的集合视图越多。

I have an infinite scroll in my UICollectionView and I've noticed that the way I estimate my cell height is the bottleneck of my collection view. It causes some long delays the more I scroll my collection view.

有更好的方法来估计我的细胞的高度吗?

单元格的高度不同,因为每个单元格都有 UILabel 。我将不同长度的NSMutableAttributedStrings分配给那些UILabel:

The cells have different heights because I have a UILabel in each of them. I assign a NSMutableAttributedStrings of different lengths to those UILabels:

let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .justified
paragraphStyle.lineSpacing = 5.0

let attributedText = NSMutableAttributedString(string: "   \(post.caption)", attributes: [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 15), .paragraphStyle: paragraphStyle, .baselineOffset: NSNumber(value: 0)])

attributedText.append(NSAttributedString(string: "\n\n", attributes: [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 4)]))

let timeAgoDisplay = post.creationDate.timeAgoDisplay()
attributedText.append(NSAttributedString(string: timeAgoDisplay, attributes: [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 14), NSAttributedStringKey.foregroundColor: UIColor.storiesLightGray()]))

captionLabel.attributedText = attributedText

我的 siz eForItemAt 方法。我注意到,当我的集合视图中有超过200个项目时,调用 dummyCell.layoutIfNeeded()会使我的应用程序变得非常慢:

My sizeForItemAt method. I've noticed that calling dummyCell.layoutIfNeeded() makes my app very slow once I have more than 200 items in my collection view:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

        var height: CGFloat = 180

        let frame = CGRect(x: 0, y: 0, width: view.frame.width, height: height)
        let dummyCell = HomePostCell(frame: frame)

        dummyCell.post = presenter.posts[indexPath.item]
        dummyCell.layoutIfNeeded()

        let targetSize = CGSize(width: view.frame.width, height: 5000)
        let estimatedSize = dummyCell.systemLayoutSizeFitting(targetSize)

        let newHeight = max(height, estimatedSize.height)

        return CGSize(width: view.frame.width, height: newHeight)
 }

谢谢!

推荐答案

理想情况下,你不应该使用 systemLayoutSizeFitting 在你的 sizeForItemAt 中,因为,正如你所说,它很慢。

Ideally you should not use systemLayoutSizeFitting in your sizeForItemAt, because, as you say, it is slow.

你可以预先缓存一些细胞数据,计算大小,并将它们存储在一个数组或类似的数组中,以便 sizeForItemAt m非常必须在数组中进行查找 - 这很快。

You could precache some cell data, calculate the sizes, and store them in an array or similar, so that sizeForItemAt merely has to do the lookup in the array — which is fast.

你真的不需要使用 systemLayoutSizeFitting ,或者;您可以使用您对标签大小和内容的了解来计算大小(例如,使用NSAttributedString测量方法)。

And you don't really need to use systemLayoutSizeFitting, either; you can use your knowledge of the label size and contents to calculate the size (e.g. using NSAttributedString measurement methods).

这是我们以前在<$ c $之前做的事情c> systemLayoutSizeFitting 或存在自动布局,它仍然快得多。

That is what we used to do before systemLayoutSizeFitting or auto layout existed, and it remains far faster.

这篇关于更快地估算CollectionView中的单元格高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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