UITableView+FDKeyedHeightCache.m 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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+FDKeyedHeightCache.h"
  23. #import <objc/runtime.h>
  24. @interface FDKeyedHeightCache ()
  25. @property (nonatomic, strong) NSMutableDictionary<id<NSCopying>, NSNumber *> *mutableHeightsByKeyForPortrait;
  26. @property (nonatomic, strong) NSMutableDictionary<id<NSCopying>, NSNumber *> *mutableHeightsByKeyForLandscape;
  27. @end
  28. @implementation FDKeyedHeightCache
  29. - (instancetype)init {
  30. self = [super init];
  31. if (self) {
  32. _mutableHeightsByKeyForPortrait = [NSMutableDictionary dictionary];
  33. _mutableHeightsByKeyForLandscape = [NSMutableDictionary dictionary];
  34. }
  35. return self;
  36. }
  37. - (NSMutableDictionary<id<NSCopying>, NSNumber *> *)mutableHeightsByKeyForCurrentOrientation {
  38. return UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation) ? self.mutableHeightsByKeyForPortrait: self.mutableHeightsByKeyForLandscape;
  39. }
  40. - (BOOL)existsHeightForKey:(id<NSCopying>)key {
  41. NSNumber *number = self.mutableHeightsByKeyForCurrentOrientation[key];
  42. return number && ![number isEqualToNumber:@-1];
  43. }
  44. - (void)cacheHeight:(CGFloat)height byKey:(id<NSCopying>)key {
  45. self.mutableHeightsByKeyForCurrentOrientation[key] = @(height);
  46. }
  47. - (CGFloat)heightForKey:(id<NSCopying>)key {
  48. #if CGFLOAT_IS_DOUBLE
  49. return [self.mutableHeightsByKeyForCurrentOrientation[key] doubleValue];
  50. #else
  51. return [self.mutableHeightsByKeyForCurrentOrientation[key] floatValue];
  52. #endif
  53. }
  54. - (void)invalidateHeightForKey:(id<NSCopying>)key {
  55. [self.mutableHeightsByKeyForPortrait removeObjectForKey:key];
  56. [self.mutableHeightsByKeyForLandscape removeObjectForKey:key];
  57. }
  58. - (void)invalidateAllHeightCache {
  59. [self.mutableHeightsByKeyForPortrait removeAllObjects];
  60. [self.mutableHeightsByKeyForLandscape removeAllObjects];
  61. }
  62. @end
  63. @implementation UITableView (FDKeyedHeightCache)
  64. - (FDKeyedHeightCache *)fd_keyedHeightCache {
  65. FDKeyedHeightCache *cache = objc_getAssociatedObject(self, _cmd);
  66. if (!cache) {
  67. cache = [FDKeyedHeightCache new];
  68. objc_setAssociatedObject(self, _cmd, cache, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  69. }
  70. return cache;
  71. }
  72. @end