cdts/xdts-ios 3/TreeHole/CYHResetCode/CYH/QMUIKit/QMUIComponents/QMUICellHeightCache.h

183 lines
12 KiB
C
Raw Normal View History

2023-07-27 09:20:00 +08:00
/**
* Tencent is pleased to support the open source community by making QMUI_iOS available.
* Copyright (C) 2016-2021 THL A29 Limited, a Tencent company. All rights reserved.
* Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
* http://opensource.org/licenses/MIT
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/
//
// QMUICellHeightCache.h
// qmui
//
// Created by QMUI Team on 15/12/23.
//
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface QMUICellHeightCache : NSObject
- (BOOL)existsHeightForKey:(id<NSCopying>)key;
- (void)cacheHeight:(CGFloat)height byKey:(id<NSCopying>)key;
- (CGFloat)heightForKey:(id<NSCopying>)key;
- (void)invalidateHeightForKey:(id<NSCopying>)key;
- (void)invalidateAllHeightCache;
@end
@interface QMUICellHeightIndexPathCache : NSObject
@property(nonatomic, assign) BOOL automaticallyInvalidateEnabled;// TODO: 这个要放在 tableView 那边
- (BOOL)existsHeightAtIndexPath:(NSIndexPath *)indexPath;
- (void)cacheHeight:(CGFloat)height byIndexPath:(NSIndexPath *)indexPath;
- (CGFloat)heightForIndexPath:(NSIndexPath *)indexPath;
- (void)invalidateHeightAtIndexPath:(NSIndexPath *)indexPath;
- (void)invalidateAllHeightCache;
@end
/// ====================== 动态计算 cell 高度相关 =======================
/**
* UITableView cell
*
* https://github.com/forkingdog/UITableView-FDTemplateLayoutCell。
*
* 1. cell sizeThatFits:
* 2. QMUITableView QMUITableViewDataSource
* 3. qmui_tableView:cellWithIdentifier: identifier cell
* 4. tableView:cellForRowAtIndexPath: 使 qmui_tableView:cellWithIdentifier: cell
* 5. tableView:heightForRowAtIndexPath: 使 UITableView (QMUILayoutCell) cell
* 6. cell UITableView qmui_invalidateXxx
*
* tableView cell cell cell key indexPath 使 cell cell cell
*
* 使 identifier cell cell
* @note tableView viewWillTransitionToSize:
*
* @note tableView 使 estimatedRowHeight使 UITableView (QMUICellHeightKeyCache)
*
* @see UITableView (QMUICellHeightKeyCache)
*/
@interface UITableView (QMUILayoutCell)
/**
* qmui_tableView:cellWithIdentifier: identifier cell configuration cell cell
* @param identifier cell identifier
* @param configuration cell block tableView:cellForRowAtIndexPath: cell
*/
- (CGFloat)qmui_heightForCellWithIdentifier:(NSString *)identifier configuration:(void (^)(__kindof UITableViewCell *cell))configuration;
/**
* qmui_tableView:cellWithIdentifier: identifier cell configuration cell cell
*
* indexPath indexPath QMUICellHeightIndexPathCache
*
* @param identifier cell identifier
* @param configuration cell block tableView:cellForRowAtIndexPath: cell
*/
- (CGFloat)qmui_heightForCellWithIdentifier:(NSString *)identifier cacheByIndexPath:(NSIndexPath *)indexPath configuration:(void (^)(__kindof UITableViewCell *cell))configuration;
/**
* qmui_tableView:cellWithIdentifier: identifier cell configuration cell cell
*
* key key QMUICellHeightCache
*
* @param identifier cell identifier
* @param configuration cell block tableView:cellForRowAtIndexPath: cell
*/
- (CGFloat)qmui_heightForCellWithIdentifier:(NSString *)identifier cacheByKey:(id<NSCopying>)key configuration:(void (^)(__kindof UITableViewCell *cell))configuration;
/// 搭配 QMUICellHeightCache清除整个列表的所有高度缓存包括 key 和 indexPath注意请不要直接使用 self.qmui_keyedHeightCache 或 self.qmui_indexPathHeightCache 的 invalidate 方法,因为一个 UITableView 在不同宽度下会有不同的 QMUICellHeightCache/QMUICellHeightIndexPathCache直接使用那两个 cache 的 invalidate 方法只能刷新当前的 cache无法刷新其他宽度下的 cache。
- (void)qmui_invalidateAllHeight;
@end
@interface UITableView (QMUIKeyedHeightCache)
/// 在 UITableView 不同的宽度下会得到不一样的 QMUICellHeightCache 实例,从而保证宽度变化时缓存自动刷新
@property(nonatomic, strong, readonly) QMUICellHeightCache *qmui_keyedHeightCache;
/// 搭配 QMUICellHeightCache清除指定 key 的高度缓存,注意请不要直接使用 [self.qmui_keyedHeightCache invalidateHeightForKey:],因为一个 UITableView 在不同宽度下会有不同的 QMUICellHeightCache直接使用那个 cache 的 invalidate 方法只能刷新当前的 cache无法刷新其他宽度下的 cache。
- (void)qmui_invalidateHeightForKey:(id<NSCopying>)key;
@end
@interface UITableView (QMUICellHeightIndexPathCache)
/// YES 表示在 reloadData、reloadIndexPath: 等方法被调用时,对应的缓存也会被自动更新,默认为 YES。仅对 indexPath 方式的缓存有效。
@property(nonatomic, assign) BOOL qmui_invalidateIndexPathHeightCachedAutomatically;
/// 在 UICollectionView 不同的大小下会得到不一样的 QMUICellHeightIndexPathCache 实例,从而保证大小变化时缓存自动刷新
@property(nonatomic, strong, readonly) QMUICellHeightIndexPathCache *qmui_indexPathHeightCache;
/// 搭配 QMUICellHeightIndexPathCache清除指定 indexPath 的高度缓存,注意请不要直接使用 [self.qmui_indexPathHeightCache invalidateHeightAtIndexPath:],因为一个 UITableView 在不同宽度下会有不同的 QMUICellHeightIndexPathCache直接使用那个 cache 的 invalidate 方法只能刷新当前的 cache无法刷新其他宽度下的 cache。
- (void)qmui_invalidateHeightAtIndexPath:(NSIndexPath *)indexPath;
@end
@interface UITableView (QMUIIndexPathHeightCacheInvalidation)
/// 当需要 reloadData 的时候,又不想使缓存失效,可以调用下面这个方法。注意,仅在 qmui_invalidateIndexPathHeightCachedAutomatically 为 YES 时才有意义。
- (void)qmui_reloadDataWithoutInvalidateIndexPathHeightCache;
@end
/// ====================== 计算动态cell高度相关 =======================
/**
* UICollectionView cell
* UITableView UITableView (QMUILayoutCell)
*/
@interface UICollectionView (QMUIKeyedHeightCache)
/// 在 UICollectionView 不同的大小下会得到不一样的 QMUICellHeightCache 实例,从而保证大小变化时缓存自动刷新
@property(nonatomic, strong, readonly) QMUICellHeightCache *qmui_keyedHeightCache;
/// 搭配 QMUICellHeightCache清除指定 key 的高度缓存,注意请不要直接使用 [self.qmui_keyedHeightCache invalidateHeightForKey:],因为一个 UICollectionView 在不同宽度下会有不同的 QMUICellHeightCache直接使用那个 cache 的 invalidate 方法只能刷新当前的 cache无法刷新其他宽度下的 cache。
- (void)qmui_invalidateHeightForKey:(id<NSCopying>)key;
@end
@interface UICollectionView (QMUICellHeightIndexPathCache)
/// YES 表示在 reloadData、reloadIndexPath: 等方法被调用时,对应的缓存也会被自动更新,默认为 YES。仅对 indexPath 方式的缓存有效。
@property(nonatomic, assign) BOOL qmui_invalidateIndexPathHeightCachedAutomatically;
/// 在 UICollectionView 不同的大小下会得到不一样的 QMUICellHeightIndexPathCache 实例,从而保证大小变化时缓存自动刷新
@property(nonatomic, strong, readonly) QMUICellHeightIndexPathCache *qmui_indexPathHeightCache;
/// 搭配 QMUICellHeightIndexPathCache清除指定 indexPath 的高度缓存,注意请不要直接使用 [self.qmui_indexPathHeightCache invalidateHeightAtIndexPath:],因为一个 UICollectionView 在不同宽度下会有不同的 QMUICellHeightIndexPathCache直接使用那个 cache 的 invalidate 方法只能刷新当前的 cache无法刷新其他宽度下的 cache。
- (void)qmui_invalidateHeightAtIndexPath:(NSIndexPath *)indexPath;
@end
@interface UICollectionView (QMUIIndexPathHeightCacheInvalidation)
/// 当需要 reloadData 的时候,又不想使缓存失效,可以调用下面这个方法。注意,仅在 qmui_invalidateIndexPathHeightCachedAutomatically 为 YES 时才有意义。
- (void)qmui_reloadDataWithoutInvalidateIndexPathHeightCache;
@end
/// 以下接口可在“sizeForItemAtIndexPath”里面调用来计算高度
/// 通过构建一个cell模拟真正显示的cell给cell设置真实的数据然后再调用cell的sizeThatFits:来计算高度
/// 也就是说我们自定义的cell里面需要重写sizeThatFits:并返回正确的值
@interface UICollectionView (QMUILayoutCell)
- (CGFloat)qmui_heightForCellWithIdentifier:(NSString *)identifier cellClass:(Class)cellClass itemWidth:(CGFloat)itemWidth configuration:(void (^)(__kindof UICollectionViewCell *cell))configuration;
// 通过indexPath缓存高度
- (CGFloat)qmui_heightForCellWithIdentifier:(NSString *)identifier cellClass:(Class)cellClass itemWidth:(CGFloat)itemWidth cacheByIndexPath:(NSIndexPath *)indexPath configuration:(void (^)(__kindof UICollectionViewCell *cell))configuration;
// 通过key缓存高度
- (CGFloat)qmui_heightForCellWithIdentifier:(NSString *)identifier cellClass:(Class)cellClass itemWidth:(CGFloat)itemWidth cacheByKey:(id<NSCopying>)key configuration:(void (^)(__kindof UICollectionViewCell *cell))configuration;
/// 搭配 QMUICellHeightCache清除整个列表的所有高度缓存包括 key 和 indexPath注意请不要直接使用 self.qmui_keyedHeightCache 或 self.qmui_indexPathHeightCache 的 invalidate 方法,因为一个 UICollectionView 在不同宽度下会有不同的 QMUICellHeightCache/QMUICellHeightIndexPathCache直接使用那两个 cache 的 invalidate 方法只能刷新当前的 cache无法刷新其他宽度下的 cache。
- (void)qmui_invalidateAllHeight;
@end