import Foundation
/// Cache the cell hight and estimated cell hight
final class CellHeightCache {
// MARK: - Properties
private var cellHeightCache = [NSManagedObjectID: CGFloat]()
private var estimatedCellHeightCache = [NSManagedObjectID: CGFloat]()
// MARK: - Height
/// Adds the height of a cell to the cache
/// - Parameters:
/// - height: Height of cell
/// - objectID: NSManagedObjectID of cell
func storeCellHeight(_ height: CGFloat, for objectID: NSManagedObjectID) {
cellHeightCache[objectID] = height
}
/// Retrieves the cell height from cache
/// - Parameter objectID: NSManagedObjectID of cell
/// - Returns: Height of cell if one can be found, else nil
func cellHeight(for objectID: NSManagedObjectID) -> CGFloat? {
cellHeightCache[objectID]
}
// MARK: - Estimated height
/// Adds the estimated height of a cell to the cache
/// - Parameters:
/// - estimatedHeight: Estimated height of cell
/// - objectID: NSManagedObjectID of cell
func storeEstimatedCellHeight(_ estimatedHeight: CGFloat, for objectID: NSManagedObjectID) {
estimatedCellHeightCache[objectID] = estimatedHeight
}
/// Retrieves the estimated cell height from cache
/// - Parameter objectID: NSManagedObjectID of cell
/// - Returns: Estimated height of cell if one can be found, else nil
func estimatedCellHeight(for objectID: NSManagedObjectID) -> CGFloat? {
estimatedCellHeightCache[objectID]
}
// MARK: - Clear
/// Clears all caches
func clear() {
cellHeightCache.removeAll()
estimatedCellHeightCache.removeAll()
}
/// Clears all caches for a given object ID
/// - Parameter objectID: NSManagedObjectID of cell
func clearCellHeightCache(for objectID: NSManagedObjectID) {
cellHeightCache.removeValue(forKey: objectID)
estimatedCellHeightCache.removeValue(forKey: objectID)
}
}
¤ Dauer der Verarbeitung: 0.3 Sekunden
¤
*© Formatika GbR, Deutschland