如何在重用发生时撤回不可见的 UICollectionViewCell 准备???
我想到的一种方法是根据布局单元格的prepareForReuse函数中的代码,但是虽然它有效,但它不是最佳的,因为它会导致比所需的更多的重新绘制。
背景:需要触发drawRect
对于方向更改后当前不可见的单元格,但会弹出以供使用且尚未重绘,所以到目前为止我只能看到prepareForReuse
会是合适的。问题是我正在重新绘制所有“重用”单元格,而我实际上只想重新绘制在设备的先前方向位置期间创建的最初弹出的单元格。
附加信息:所以目前我正在这样做:
在视图控制器中:
override func viewWillLayoutSubviews() {
// Clear cached layout attributes (to ensure new positions are calculated)
(self.cal.collectionViewLayout as! GCCalendarLayout).resetCache()
self.cal.collectionViewLayout.invalidateLayout()
// Trigger cells to redraw themselves (to get new widths etc)
for cell in self.cal?.visibleCells() as! [GCCalendarCell] {
cell.setNeedsDisplay()
}
// Not sure how to "setNeedsDisplay" on non visible cells here?
}
在布局单元类中:
override func prepareForReuse() {
super.prepareForReuse()
// Ensure "drawRect" is called (only way I could see to handle change in orientation
self.setNeedsDisplay()
// ISSUE: It does this also for subsequent "prepareForReuse" after all
// non-visible cells have been re-used and re-drawn, so really
// not optimal
}
如果没有上面的prepareForReuse 中的代码,会发生什么情况的示例。方向改变后以及向上滚动一点后拍摄的快照:
我想我现在在这里:
import UIKit
@IBDesignable class GCCalendarCell: UICollectionViewCell {
var prevBounds : CGRect?
override func layoutSubviews() {
if let prevBounds = prevBounds {
if !( (prevBounds.width == bounds.width) && (prevBounds.height == bounds.height) ) {
self.setNeedsDisplay()
}
}
}
override func drawRect(rect: CGRect) {
// Do Stuff
self.prevBounds = self.bounds
}
}
注意到此检查在“prepareForReuse”中不起作用,因为此时单元尚未应用旋转。然而似乎在“layoutSubviews”中工作。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)