UITableView+FDIndexPathHeightCache.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. // The MIT License (MIT)
  2. //
  3. // Copyright (c) 2015-2016 forkingdog ( https://github.com/forkingdog )
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in all
  13. // copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. // SOFTWARE.
  22. #import "UITableView+FDIndexPathHeightCache.h"
  23. #import <objc/runtime.h>
  24. typedef NSMutableArray<NSMutableArray<NSNumber *> *> FDIndexPathHeightsBySection;
  25. @interface FDIndexPathHeightCache ()
  26. @property (nonatomic, strong) FDIndexPathHeightsBySection *heightsBySectionForPortrait;
  27. @property (nonatomic, strong) FDIndexPathHeightsBySection *heightsBySectionForLandscape;
  28. @end
  29. @implementation FDIndexPathHeightCache
  30. - (instancetype)init {
  31. self = [super init];
  32. if (self) {
  33. _heightsBySectionForPortrait = [NSMutableArray array];
  34. _heightsBySectionForLandscape = [NSMutableArray array];
  35. }
  36. return self;
  37. }
  38. - (FDIndexPathHeightsBySection *)heightsBySectionForCurrentOrientation {
  39. return UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation) ? self.heightsBySectionForPortrait: self.heightsBySectionForLandscape;
  40. }
  41. - (void)enumerateAllOrientationsUsingBlock:(void (^)(FDIndexPathHeightsBySection *heightsBySection))block {
  42. block(self.heightsBySectionForPortrait);
  43. block(self.heightsBySectionForLandscape);
  44. }
  45. - (BOOL)existsHeightAtIndexPath:(NSIndexPath *)indexPath {
  46. [self buildCachesAtIndexPathsIfNeeded:@[indexPath]];
  47. NSNumber *number = self.heightsBySectionForCurrentOrientation[indexPath.section][indexPath.row];
  48. return ![number isEqualToNumber:@-1];
  49. }
  50. - (void)cacheHeight:(CGFloat)height byIndexPath:(NSIndexPath *)indexPath {
  51. self.automaticallyInvalidateEnabled = YES;
  52. [self buildCachesAtIndexPathsIfNeeded:@[indexPath]];
  53. self.heightsBySectionForCurrentOrientation[indexPath.section][indexPath.row] = @(height);
  54. }
  55. - (CGFloat)heightForIndexPath:(NSIndexPath *)indexPath {
  56. [self buildCachesAtIndexPathsIfNeeded:@[indexPath]];
  57. NSNumber *number = self.heightsBySectionForCurrentOrientation[indexPath.section][indexPath.row];
  58. #if CGFLOAT_IS_DOUBLE
  59. return number.doubleValue;
  60. #else
  61. return number.floatValue;
  62. #endif
  63. }
  64. - (void)invalidateHeightAtIndexPath:(NSIndexPath *)indexPath {
  65. [self buildCachesAtIndexPathsIfNeeded:@[indexPath]];
  66. [self enumerateAllOrientationsUsingBlock:^(FDIndexPathHeightsBySection *heightsBySection) {
  67. heightsBySection[indexPath.section][indexPath.row] = @-1;
  68. }];
  69. }
  70. - (void)invalidateAllHeightCache {
  71. [self enumerateAllOrientationsUsingBlock:^(FDIndexPathHeightsBySection *heightsBySection) {
  72. [heightsBySection removeAllObjects];
  73. }];
  74. }
  75. - (void)buildCachesAtIndexPathsIfNeeded:(NSArray *)indexPaths {
  76. // Build every section array or row array which is smaller than given index path.
  77. [indexPaths enumerateObjectsUsingBlock:^(NSIndexPath *indexPath, NSUInteger idx, BOOL *stop) {
  78. [self buildSectionsIfNeeded:indexPath.section];
  79. [self buildRowsIfNeeded:indexPath.row inExistSection:indexPath.section];
  80. }];
  81. }
  82. - (void)buildSectionsIfNeeded:(NSInteger)targetSection {
  83. [self enumerateAllOrientationsUsingBlock:^(FDIndexPathHeightsBySection *heightsBySection) {
  84. for (NSInteger section = 0; section <= targetSection; ++section) {
  85. if (section >= heightsBySection.count) {
  86. heightsBySection[section] = [NSMutableArray array];
  87. }
  88. }
  89. }];
  90. }
  91. - (void)buildRowsIfNeeded:(NSInteger)targetRow inExistSection:(NSInteger)section {
  92. [self enumerateAllOrientationsUsingBlock:^(FDIndexPathHeightsBySection *heightsBySection) {
  93. NSMutableArray<NSNumber *> *heightsByRow = heightsBySection[section];
  94. for (NSInteger row = 0; row <= targetRow; ++row) {
  95. if (row >= heightsByRow.count) {
  96. heightsByRow[row] = @-1;
  97. }
  98. }
  99. }];
  100. }
  101. @end
  102. @implementation UITableView (FDIndexPathHeightCache)
  103. - (FDIndexPathHeightCache *)fd_indexPathHeightCache {
  104. FDIndexPathHeightCache *cache = objc_getAssociatedObject(self, _cmd);
  105. if (!cache) {
  106. cache = [FDIndexPathHeightCache new];
  107. objc_setAssociatedObject(self, _cmd, cache, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  108. }
  109. return cache;
  110. }
  111. @end
  112. // We just forward primary call, in crash report, top most method in stack maybe FD's,
  113. // but it's really not our bug, you should check whether your table view's data source and
  114. // displaying cells are not matched when reloading.
  115. static void __FD_TEMPLATE_LAYOUT_CELL_PRIMARY_CALL_IF_CRASH_NOT_OUR_BUG__(void (^callout)(void)) {
  116. callout();
  117. }
  118. #define FDPrimaryCall(...) do {__FD_TEMPLATE_LAYOUT_CELL_PRIMARY_CALL_IF_CRASH_NOT_OUR_BUG__(^{__VA_ARGS__});} while(0)
  119. @implementation UITableView (FDIndexPathHeightCacheInvalidation)
  120. - (void)fd_reloadDataWithoutInvalidateIndexPathHeightCache {
  121. FDPrimaryCall([self fd_reloadData];);
  122. }
  123. + (void)load {
  124. // All methods that trigger height cache's invalidation
  125. SEL selectors[] = {
  126. @selector(reloadData),
  127. @selector(insertSections:withRowAnimation:),
  128. @selector(deleteSections:withRowAnimation:),
  129. @selector(reloadSections:withRowAnimation:),
  130. @selector(moveSection:toSection:),
  131. @selector(insertRowsAtIndexPaths:withRowAnimation:),
  132. @selector(deleteRowsAtIndexPaths:withRowAnimation:),
  133. @selector(reloadRowsAtIndexPaths:withRowAnimation:),
  134. @selector(moveRowAtIndexPath:toIndexPath:)
  135. };
  136. for (NSUInteger index = 0; index < sizeof(selectors) / sizeof(SEL); ++index) {
  137. SEL originalSelector = selectors[index];
  138. SEL swizzledSelector = NSSelectorFromString([@"fd_" stringByAppendingString:NSStringFromSelector(originalSelector)]);
  139. Method originalMethod = class_getInstanceMethod(self, originalSelector);
  140. Method swizzledMethod = class_getInstanceMethod(self, swizzledSelector);
  141. method_exchangeImplementations(originalMethod, swizzledMethod);
  142. }
  143. }
  144. - (void)fd_reloadData {
  145. if (self.fd_indexPathHeightCache.automaticallyInvalidateEnabled) {
  146. [self.fd_indexPathHeightCache enumerateAllOrientationsUsingBlock:^(FDIndexPathHeightsBySection *heightsBySection) {
  147. [heightsBySection removeAllObjects];
  148. }];
  149. }
  150. FDPrimaryCall([self fd_reloadData];);
  151. }
  152. - (void)fd_insertSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation {
  153. if (self.fd_indexPathHeightCache.automaticallyInvalidateEnabled) {
  154. [sections enumerateIndexesUsingBlock:^(NSUInteger section, BOOL *stop) {
  155. [self.fd_indexPathHeightCache buildSectionsIfNeeded:section];
  156. [self.fd_indexPathHeightCache enumerateAllOrientationsUsingBlock:^(FDIndexPathHeightsBySection *heightsBySection) {
  157. [heightsBySection insertObject:[NSMutableArray array] atIndex:section];
  158. }];
  159. }];
  160. }
  161. FDPrimaryCall([self fd_insertSections:sections withRowAnimation:animation];);
  162. }
  163. - (void)fd_deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation {
  164. if (self.fd_indexPathHeightCache.automaticallyInvalidateEnabled) {
  165. [sections enumerateIndexesUsingBlock:^(NSUInteger section, BOOL *stop) {
  166. [self.fd_indexPathHeightCache buildSectionsIfNeeded:section];
  167. [self.fd_indexPathHeightCache enumerateAllOrientationsUsingBlock:^(FDIndexPathHeightsBySection *heightsBySection) {
  168. [heightsBySection removeObjectAtIndex:section];
  169. }];
  170. }];
  171. }
  172. FDPrimaryCall([self fd_deleteSections:sections withRowAnimation:animation];);
  173. }
  174. - (void)fd_reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation {
  175. if (self.fd_indexPathHeightCache.automaticallyInvalidateEnabled) {
  176. [sections enumerateIndexesUsingBlock: ^(NSUInteger section, BOOL *stop) {
  177. [self.fd_indexPathHeightCache buildSectionsIfNeeded:section];
  178. [self.fd_indexPathHeightCache enumerateAllOrientationsUsingBlock:^(FDIndexPathHeightsBySection *heightsBySection) {
  179. [heightsBySection[section] removeAllObjects];
  180. }];
  181. }];
  182. }
  183. FDPrimaryCall([self fd_reloadSections:sections withRowAnimation:animation];);
  184. }
  185. - (void)fd_moveSection:(NSInteger)section toSection:(NSInteger)newSection {
  186. if (self.fd_indexPathHeightCache.automaticallyInvalidateEnabled) {
  187. [self.fd_indexPathHeightCache buildSectionsIfNeeded:section];
  188. [self.fd_indexPathHeightCache buildSectionsIfNeeded:newSection];
  189. [self.fd_indexPathHeightCache enumerateAllOrientationsUsingBlock:^(FDIndexPathHeightsBySection *heightsBySection) {
  190. [heightsBySection exchangeObjectAtIndex:section withObjectAtIndex:newSection];
  191. }];
  192. }
  193. FDPrimaryCall([self fd_moveSection:section toSection:newSection];);
  194. }
  195. - (void)fd_insertRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation {
  196. if (self.fd_indexPathHeightCache.automaticallyInvalidateEnabled) {
  197. [self.fd_indexPathHeightCache buildCachesAtIndexPathsIfNeeded:indexPaths];
  198. [indexPaths enumerateObjectsUsingBlock:^(NSIndexPath *indexPath, NSUInteger idx, BOOL *stop) {
  199. [self.fd_indexPathHeightCache enumerateAllOrientationsUsingBlock:^(FDIndexPathHeightsBySection *heightsBySection) {
  200. [heightsBySection[indexPath.section] insertObject:@-1 atIndex:indexPath.row];
  201. }];
  202. }];
  203. }
  204. FDPrimaryCall([self fd_insertRowsAtIndexPaths:indexPaths withRowAnimation:animation];);
  205. }
  206. - (void)fd_deleteRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation {
  207. if (self.fd_indexPathHeightCache.automaticallyInvalidateEnabled) {
  208. [self.fd_indexPathHeightCache buildCachesAtIndexPathsIfNeeded:indexPaths];
  209. NSMutableDictionary<NSNumber *, NSMutableIndexSet *> *mutableIndexSetsToRemove = [NSMutableDictionary dictionary];
  210. [indexPaths enumerateObjectsUsingBlock:^(NSIndexPath *indexPath, NSUInteger idx, BOOL *stop) {
  211. NSMutableIndexSet *mutableIndexSet = mutableIndexSetsToRemove[@(indexPath.section)];
  212. if (!mutableIndexSet) {
  213. mutableIndexSet = [NSMutableIndexSet indexSet];
  214. mutableIndexSetsToRemove[@(indexPath.section)] = mutableIndexSet;
  215. }
  216. [mutableIndexSet addIndex:indexPath.row];
  217. }];
  218. [mutableIndexSetsToRemove enumerateKeysAndObjectsUsingBlock:^(NSNumber *key, NSIndexSet *indexSet, BOOL *stop) {
  219. [self.fd_indexPathHeightCache enumerateAllOrientationsUsingBlock:^(FDIndexPathHeightsBySection *heightsBySection) {
  220. [heightsBySection[key.integerValue] removeObjectsAtIndexes:indexSet];
  221. }];
  222. }];
  223. }
  224. FDPrimaryCall([self fd_deleteRowsAtIndexPaths:indexPaths withRowAnimation:animation];);
  225. }
  226. - (void)fd_reloadRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation {
  227. if (self.fd_indexPathHeightCache.automaticallyInvalidateEnabled) {
  228. [self.fd_indexPathHeightCache buildCachesAtIndexPathsIfNeeded:indexPaths];
  229. [indexPaths enumerateObjectsUsingBlock:^(NSIndexPath *indexPath, NSUInteger idx, BOOL *stop) {
  230. [self.fd_indexPathHeightCache enumerateAllOrientationsUsingBlock:^(FDIndexPathHeightsBySection *heightsBySection) {
  231. heightsBySection[indexPath.section][indexPath.row] = @-1;
  232. }];
  233. }];
  234. }
  235. FDPrimaryCall([self fd_reloadRowsAtIndexPaths:indexPaths withRowAnimation:animation];);
  236. }
  237. - (void)fd_moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
  238. if (self.fd_indexPathHeightCache.automaticallyInvalidateEnabled) {
  239. [self.fd_indexPathHeightCache buildCachesAtIndexPathsIfNeeded:@[sourceIndexPath, destinationIndexPath]];
  240. [self.fd_indexPathHeightCache enumerateAllOrientationsUsingBlock:^(FDIndexPathHeightsBySection *heightsBySection) {
  241. NSMutableArray<NSNumber *> *sourceRows = heightsBySection[sourceIndexPath.section];
  242. NSMutableArray<NSNumber *> *destinationRows = heightsBySection[destinationIndexPath.section];
  243. NSNumber *sourceValue = sourceRows[sourceIndexPath.row];
  244. NSNumber *destinationValue = destinationRows[destinationIndexPath.row];
  245. sourceRows[sourceIndexPath.row] = destinationValue;
  246. destinationRows[destinationIndexPath.row] = sourceValue;
  247. }];
  248. }
  249. FDPrimaryCall([self fd_moveRowAtIndexPath:sourceIndexPath toIndexPath:destinationIndexPath];);
  250. }
  251. @end