如何在重用发生时重绘不可见的 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 中的代码会发生什么的示例。在方向更改后拍摄的快照,并且在向上滚动一点后拍摄: