线上所有马甲包模板,与《猎豆》同UI。域名zhuadd

UITableView+SDAutoTableViewCellHeight.h 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. //
  2. // UITableView+SDAutoTableViewCellHeight.h
  3. // SDAutoLayout 测试 Demo
  4. //
  5. // Created by aier on 15/11/1.
  6. // Copyright © 2015年 gsd. All rights reserved.
  7. //
  8. /*
  9. *********************************************************************************
  10. * *
  11. * 在您使用此自动布局库的过程中如果出现bug请及时以以下任意一种方式联系我们,我们会及时修复bug并 *
  12. * 帮您解决问题。 *
  13. * QQ : 2689718696(gsdios) *
  14. * Email : gsdios@126.com *
  15. * GitHub: https://github.com/gsdios *
  16. * 新浪微博:GSD_iOS *
  17. * *
  18. *********************************************************************************
  19. */
  20. /*
  21. PS:cell高度自适应前提>>应该调用cell的“- (void)setupAutoHeightWithBottomView:(UIView *)bottomView bottomMargin:(CGFloat)bottomMargin”方法进行cell的自动高度设置
  22. */
  23. #import <UIKit/UIKit.h>
  24. #import "UIView+SDAutoLayout.h"
  25. @class SDCellAutoHeightManager;
  26. typedef void (^AutoCellHeightDataSettingBlock)(UITableViewCell *cell);
  27. #define kSDModelCellTag 199206
  28. #pragma mark - UITableView 方法,返回自动计算出的cell高度
  29. @interface UITableView (SDAutoTableViewCellHeight)
  30. @property (nonatomic, strong) SDCellAutoHeightManager *cellAutoHeightManager;
  31. /**
  32. * 返回计算出的cell高度(普通简化版方法,同样只需一步设置即可完成)(用法:单cell详见demo5,多cell详见demo7)
  33. * model : cell的数据模型实例
  34. * keyPath : cell的数据模型属性的属性名字符串(即kvc原理中的key)
  35. * cellClass : 当前的indexPath对应的cell的class
  36. * contentViewWidth : cell的contentView的宽度
  37. */
  38. - (CGFloat)cellHeightForIndexPath:(NSIndexPath *)indexPath model:(id)model keyPath:(NSString *)keyPath cellClass:(Class)cellClass contentViewWidth:(CGFloat)contentViewWidth;
  39. /**
  40. * 返回计算出的cell高度(普通简化版方法,同样只需一步设置即可完成)(用法:见DemoVC14)
  41. * cellClass : 当前的indexPath对应的cell的class
  42. * contentViewWidth : cell的contentView的宽度
  43. * cellDataSetting : 设置cell数据的block
  44. */
  45. - (CGFloat)cellHeightForIndexPath:(NSIndexPath *)indexPath cellClass:(Class)cellClass cellContentViewWidth:(CGFloat)width cellDataSetting:(AutoCellHeightDataSettingBlock)cellDataSetting;
  46. /** 刷新tableView但不清空之前已经计算好的高度缓存,用于直接将新数据拼接在旧数据之后的tableView刷新 */
  47. - (void)reloadDataWithExistedHeightCache;
  48. /** 刷新tableView同时调整已经计算好的高度缓存,用于直接将新数据插在旧数据前面的tableView的刷新 */
  49. - (void)reloadDataWithInsertingDataAtTheBeginingOfSection:(NSInteger)section newDataCount:(NSInteger)count;
  50. /**
  51. * 刷新tableView同时调整已经计算好的高度缓存,用于直接将新数据插在旧数据前面的tableView的刷新(用于刷新多个section)
  52. * sectionNumsArray : 要刷新的所有section序号组成的数组, 例@[@(0), @(1)]
  53. * dataCountsArray : 每个section的数据条数组成的数组, 例@[@(20), @(10)]
  54. */
  55. - (void)reloadDataWithInsertingDataAtTheBeginingOfSections:(NSArray *)sectionNumsArray newDataCounts:(NSArray *)dataCountsArray;
  56. /** 返回所有cell的高度总和 */
  57. - (CGFloat)cellsTotalHeight;
  58. @property (nonatomic, copy) AutoCellHeightDataSettingBlock cellDataSetting;
  59. @end
  60. #pragma mark - UITableViewController 方法,返回自动计算出的cell高度
  61. @interface UITableViewController (SDTableViewControllerAutoCellHeight)
  62. /** (UITableViewController方法)升级版!一行代码(一步设置)搞定tableview的cell高度自适应,同时适用于单cell和多cell,性能比普通版稍微差一些,不建议在数据量大的tableview中使用 */
  63. - (CGFloat)cellHeightForIndexPath:(NSIndexPath *)indexPath cellContentViewWidth:(CGFloat)width;
  64. @end
  65. #pragma mark - NSObject 方法,返回自动计算出的cell高度
  66. @interface NSObject (SDAnyObjectAutoCellHeight)
  67. /** (NSObject方法)升级版!一行代码(一步设置)搞定tableview的cell高度自适应,同时适用于单cell和多cell,性能比普通版稍微差一些,不建议在数据量大的tableview中使用 */
  68. - (CGFloat)cellHeightForIndexPath:(NSIndexPath *)indexPath cellContentViewWidth:(CGFloat)width tableView:(UITableView *)tableView;
  69. @end
  70. // ------------------------------- 以下为库内部使用无须了解 --------------------
  71. @interface SDCellAutoHeightManager : NSObject
  72. @property (nonatomic, assign) BOOL shouldKeepHeightCacheWhenReloadingData;
  73. @property (nonatomic, assign) CGFloat contentViewWidth;
  74. @property (nonatomic, assign) Class cellClass;
  75. @property (nonatomic, assign) CGFloat cellHeight;
  76. @property (nonatomic, strong) UITableViewCell *modelCell;
  77. @property (nonatomic, strong) NSMutableDictionary *subviewFrameCacheDict;
  78. @property (nonatomic, strong, readonly) NSDictionary *heightCacheDict;
  79. @property (nonatomic, copy) AutoCellHeightDataSettingBlock cellDataSetting;
  80. - (void)clearHeightCache;
  81. - (void)clearHeightCacheOfIndexPaths:(NSArray *)indexPaths;
  82. - (void)deleteThenResetHeightCache:(NSIndexPath *)indexPathToDelete;
  83. - (void)insertNewDataAtTheBeginingOfSection:(NSInteger)section newDataCount:(NSInteger)count;
  84. - (void)insertNewDataAtIndexPaths:(NSArray *)indexPaths;
  85. - (NSNumber *)heightCacheForIndexPath:(NSIndexPath *)indexPath;
  86. - (CGFloat)cellHeightForIndexPath:(NSIndexPath *)indexPath model:(id)model keyPath:(NSString *)keyPath;
  87. - (CGFloat)cellHeightForIndexPath:(NSIndexPath *)indexPath model:(id)model keyPath:(NSString *)keyPath cellClass:(Class)cellClass;
  88. - (NSMutableArray *)subviewFrameCachesWithIndexPath:(NSIndexPath *)indexPath;;
  89. - (void)setSubviewFrameCache:(CGRect)rect WithIndexPath:(NSIndexPath *)indexPath;
  90. - (instancetype)initWithCellClass:(Class)cellClass tableView:(UITableView *)tableView;
  91. + (instancetype)managerWithCellClass:(Class)cellClass tableView:(UITableView *)tableView;
  92. @end