6

再利用が発生したときに、非表示の UICollectionViewCell を再描画する方法???

私が考えた 1 つのアプローチは、レイアウト セルの 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?
}

レイアウト セル クラス:

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 のコードがないとどうなるかの例。向きを変えて少し上にスクロールした直後のスナップショット:

ここに画像の説明を入力

4

3 に答える 3