Gre*_*reg 5 ios uicollectionview uicollectionviewcell swift prepareforreuse
如何在重用发生时重绘不可见的UICollectionViewCell?
我想到的一种方法是使用Layout Cell prepareForReuse函数中的代码,但是它的工作方式非理想,因为它会导致需要更多的重新绘制.
背景:需要drawRect在方向更改后触发单元格,这些方向更改当前不可见,但弹出使用并且尚未重绘,所以到目前为止我只能看到这prepareForReuse是合适的.问题是我正在重新绘制所有"重用"单元格,而我真的只想重绘最初弹出的那些在设备的前一个方向位置创建的单元格.
附加信息:所以目前我这样做:
在ViewController中:
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?
}
Run Code Online (Sandbox Code Playgroud)
在Layout Cell类中:
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
}
Run Code Online (Sandbox Code Playgroud)
没有上面的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
}
}
Run Code Online (Sandbox Code Playgroud)
注意到此检查在“prepareForReuse”中不起作用,因为此时单元格尚未应用旋转。然而,似乎在“layoutSubviews”中工作。
| 归档时间: |
|
| 查看次数: |
2329 次 |
| 最近记录: |