[英]Actual performance of UICollectionView is really bad
I am currently working on a project and basically, I am still learning. 我目前正在从事一个项目,基本上,我仍在学习。
I was using at the beginning a UITableView
, but I needed automatic height and 2 or 3 columns (depending on the device). 我在一开始使用
UITableView
,但是我需要自动设置高度和2或3列(取决于设备)。 For the columns, I am using UICollectionViewFlowLayout
. 对于列,我正在使用
UICollectionViewFlowLayout
。 In general, I am using only Labels
and StackViews
; 通常,我只使用
Labels
和StackViews
; a few warnings like Unable to simultaneously satisfy constraints
(but I changed in a few ones priorities and is working but the performance is still really bad. 诸如
Unable to simultaneously satisfy constraints
的一些警告(但我更改了一些优先级,并且可以正常工作,但性能仍然很差。
I am not retrieving info from backend, nothing spectacular. 我不是从后端检索信息,没有什么特别的。
The CustomLayout
as follows: CustomLayout
如下:
public protocol CollectionViewFlowLayoutDelegate: class {
func numberOfColumns() -> Int
func height(at indexPath: IndexPath) -> CGFloat
}
public class CustomLayout: UICollectionViewFlowLayout {
private let cellPadding: CGFloat = 4
public var cache: [IndexPath : UICollectionViewLayoutAttributes] = [:]
private var contentHeight: CGFloat = 0
private var contentWidth: CGFloat {
guard let collectionView = collectionView else {
return 0
}
let insets = collectionView.contentInset
return collectionView.bounds.width - (insets.left + insets.right)
}
public weak var flowDelegate: CollectionViewFlowLayoutDelegate?
public override var collectionViewContentSize: CGSize {
return CGSize(width: self.contentWidth, height: self.contentHeight)
}
public override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
var layoutAttributesArray = [UICollectionViewLayoutAttributes]()
if cache.isEmpty {
self.prepare()
}
for (_, layoutAttributes) in self.cache {
if rect.intersects(layoutAttributes.frame) {
layoutAttributesArray.append(layoutAttributes)
}
}
return layoutAttributesArray
}
public override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
return self.cache[indexPath]
}
public override func prepare() {
super.prepare()
cache.removeAll()
guard let collectionView = self.collectionView else {
return
}
let numberOfColumns = self.flowDelegate?.numberOfColumns() ?? 1
self.contentHeight = 0
let columnWidth = UIScreen.main.bounds.width / CGFloat(numberOfColumns) - CGFloat(numberOfColumns-1) * cellPadding
var xOffset = [CGFloat]()
for column in 0 ..< numberOfColumns {
xOffset.append(CGFloat(column) * columnWidth)
}
var column = 0
var yOffset = [CGFloat](repeating: 0, count: numberOfColumns)
for item in 0 ..< collectionView.numberOfItems(inSection: 0) {
let indexPath = IndexPath(item: item, section: 0)
let photoHeight = self.flowDelegate?.height(at: indexPath) ?? 1
let height = cellPadding * 2 + photoHeight
let frame = CGRect(x: xOffset[column], y: yOffset[column], width: columnWidth, height: height)
let insetFrame = frame.insetBy(dx: cellPadding, dy: cellPadding)
let attributes = UICollectionViewLayoutAttributes(forCellWith: indexPath)
attributes.frame = insetFrame
self.cache[indexPath] = attributes
self.contentHeight = max(self.contentHeight, frame.maxY)
yOffset[column] = yOffset[column] + height
column = column < (numberOfColumns - 1) ? (column + 1) : 0
}
}
public override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
return true
}
}
I do need the 2 or 3 columns with the automatic height. 我确实需要2或3列具有自动高度的列。 But what is really important as in any other project:
Performance
. 但是,与任何其他项目一样,真正重要的是:
Performance
。
Thank you very much in advance. 提前非常感谢您。
事实证明,在ViewControllers
,在func height(at indexPath: IndexPath) -> CGFloat
protocol
,我每次都在计算高度,而不是仅从数组中检索它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.