【发布时间】:2019-01-09 18:40:05
【问题描述】:
我目前正在做一个项目,基本上,我还在学习。
我一开始使用的是UITableView,但我需要自动高度和 2 或 3 列(取决于设备)。
对于列,我使用UICollectionViewFlowLayout。
一般来说,我只使用Labels和StackViews;像Unable to simultaneously satisfy constraints 这样的一些警告(但我改变了一些优先级并且正在工作,但性能仍然非常糟糕。
我没有从后端检索信息,没什么了不起的。
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
}
}
我确实需要具有自动高度的 2 或 3 列。但与任何其他项目一样,真正重要的是:Performance。
非常感谢您。
【问题讨论】:
-
请在您的问题正文中包含任何相关代码,而不是作为外部链接。
-
我添加了
CustomLayout,可能是性能问题... -
感谢您提供代码。很多人不能访问外部链接,这也很烦人,因为谁知道这些链接去哪里了,或者它们是否会在以后过期。
-
好的,谢谢您的建议。我是否应该删除
[link](google.com)并最好将链接保留为:google.com? -
链接适用于某些事物,例如参考资料、包或其他事物。它们不适合您正在处理的代码。这里的目标是使您的问题尽可能独立。外部链接可能并且将会过期或更改,因此保持问题的基本部分完好无损非常重要。
标签: swift performance uicollectionview uicollectionviewcell uicollectionviewflowlayout