UITableView+FDTemplateLayoutCell.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 <UIKit/UIKit.h>
  23. #import "UITableView+FDKeyedHeightCache.h"
  24. #import "UITableView+FDIndexPathHeightCache.h"
  25. #import "UITableView+FDTemplateLayoutCellDebug.h"
  26. @interface UITableView (FDTemplateLayoutCell)
  27. /// Access to internal template layout cell for given reuse identifier.
  28. /// Generally, you don't need to know these template layout cells.
  29. ///
  30. /// @param identifier Reuse identifier for cell which must be registered.
  31. ///
  32. - (__kindof UITableViewCell *)fd_templateCellForReuseIdentifier:(NSString *)identifier;
  33. /// Returns height of cell of type specifed by a reuse identifier and configured
  34. /// by the configuration block.
  35. ///
  36. /// The cell would be layed out on a fixed-width, vertically expanding basis with
  37. /// respect to its dynamic content, using auto layout. Thus, it is imperative that
  38. /// the cell was set up to be self-satisfied, i.e. its content always determines
  39. /// its height given the width is equal to the tableview's.
  40. ///
  41. /// @param identifier A string identifier for retrieving and maintaining template
  42. /// cells with system's "-dequeueReusableCellWithIdentifier:" call.
  43. /// @param configuration An optional block for configuring and providing content
  44. /// to the template cell. The configuration should be minimal for scrolling
  45. /// performance yet sufficient for calculating cell's height.
  46. ///
  47. - (CGFloat)fd_heightForCellWithIdentifier:(NSString *)identifier configuration:(void (^)(id cell))configuration;
  48. /// This method does what "-fd_heightForCellWithIdentifier:configuration" does, and
  49. /// calculated height will be cached by its index path, returns a cached height
  50. /// when needed. Therefore lots of extra height calculations could be saved.
  51. ///
  52. /// No need to worry about invalidating cached heights when data source changes, it
  53. /// will be done automatically when you call "-reloadData" or any method that triggers
  54. /// UITableView's reloading.
  55. ///
  56. /// @param indexPath where this cell's height cache belongs.
  57. ///
  58. - (CGFloat)fd_heightForCellWithIdentifier:(NSString *)identifier cacheByIndexPath:(NSIndexPath *)indexPath configuration:(void (^)(id cell))configuration;
  59. /// This method caches height by your model entity's identifier.
  60. /// If your model's changed, call "-invalidateHeightForKey:(id <NSCopying>)key" to
  61. /// invalidate cache and re-calculate, it's much cheaper and effective than "cacheByIndexPath".
  62. ///
  63. /// @param key model entity's identifier whose data configures a cell.
  64. ///
  65. - (CGFloat)fd_heightForCellWithIdentifier:(NSString *)identifier cacheByKey:(id<NSCopying>)key configuration:(void (^)(id cell))configuration;
  66. @end
  67. @interface UITableView (FDTemplateLayoutHeaderFooterView)
  68. /// Returns header or footer view's height that registered in table view with reuse identifier.
  69. ///
  70. /// Use it after calling "-[UITableView registerNib/Class:forHeaderFooterViewReuseIdentifier]",
  71. /// same with "-fd_heightForCellWithIdentifier:configuration:", it will call "-sizeThatFits:" for
  72. /// subclass of UITableViewHeaderFooterView which is not using Auto Layout.
  73. ///
  74. - (CGFloat)fd_heightForHeaderFooterViewWithIdentifier:(NSString *)identifier configuration:(void (^)(id headerFooterView))configuration;
  75. @end
  76. @interface UITableViewCell (FDTemplateLayoutCell)
  77. /// Indicate this is a template layout cell for calculation only.
  78. /// You may need this when there are non-UI side effects when configure a cell.
  79. /// Like:
  80. /// - (void)configureCell:(FooCell *)cell atIndexPath:(NSIndexPath *)indexPath {
  81. /// cell.entity = [self entityAtIndexPath:indexPath];
  82. /// if (!cell.fd_isTemplateLayoutCell) {
  83. /// [self notifySomething]; // non-UI side effects
  84. /// }
  85. /// }
  86. ///
  87. @property (nonatomic, assign) BOOL fd_isTemplateLayoutCell;
  88. /// Enable to enforce this template layout cell to use "frame layout" rather than "auto layout",
  89. /// and will ask cell's height by calling "-sizeThatFits:", so you must override this method.
  90. /// Use this property only when you want to manually control this template layout cell's height
  91. /// calculation mode, default to NO.
  92. ///
  93. @property (nonatomic, assign) BOOL fd_enforceFrameLayout;
  94. @end