FLEXTableViewSection.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. //
  2. // FLEXTableViewSection.h
  3. // FLEX
  4. //
  5. // Created by Tanner on 1/29/20.
  6. // Copyright © 2020 Flipboard. All rights reserved.
  7. //
  8. #import "FLEXUtility.h"
  9. @class FLEXTableView;
  10. NS_ASSUME_NONNULL_BEGIN
  11. #pragma mark FLEXTableViewSection
  12. /// An abstract base class for table view sections.
  13. ///
  14. /// Many properties or methods here return nil or some logical equivalent by default.
  15. /// Even so, most of the methods with defaults are intended to be overriden by subclasses.
  16. /// Some methods are not implemented at all and MUST be implemented by a subclass.
  17. @interface FLEXTableViewSection : NSObject {
  18. @protected
  19. /// Unused by default, use if you want
  20. NSString *_title;
  21. }
  22. #pragma mark - Data
  23. /// A title to be displayed for the custom section.
  24. /// Subclasses may override or use the \c _title ivar.
  25. @property (nonatomic, readonly, nullable) NSString *title;
  26. /// The number of rows in this section. Subclasses must override.
  27. /// This should not change until \c filterText is changed or \c reloadData is called.
  28. @property (nonatomic, readonly) NSInteger numberOfRows;
  29. /// A map of reuse identifiers to \c UITableViewCell (sub)class objects.
  30. /// Subclasses \e may override this as necessary, but are not required to.
  31. /// See \c FLEXTableView.h for more information.
  32. /// @return nil by default.
  33. @property (nonatomic, readonly, nullable) NSDictionary<NSString *, Class> *cellRegistrationMapping;
  34. /// The section should filter itself based on the contents of this property
  35. /// as it is set. If it is set to nil or an empty string, it should not filter.
  36. /// Subclasses should override or observe this property and react to changes.
  37. ///
  38. /// It is common practice to use two arrays for the underlying model:
  39. /// One to hold all rows, and one to hold unfiltered rows. When \c setFilterText:
  40. /// is called, call \c super to store the new value, and re-filter your model accordingly.
  41. @property (nonatomic, nullable) NSString *filterText;
  42. /// Provides an avenue for the section to refresh data or change the number of rows.
  43. ///
  44. /// This is called before reloading the table view itself. If your section pulls data
  45. /// from an external data source, this is a good place to refresh that data entirely.
  46. /// If your section does not, then it might be simpler for you to just override
  47. /// \c setFilterText: to call \c super and call \c reloadData.
  48. - (void)reloadData;
  49. #pragma mark - Row Selection
  50. /// Whether the given row should be selectable, such as if tapping the cell
  51. /// should take the user to a new screen or trigger an action.
  52. /// Subclasses \e may override this as necessary, but are not required to.
  53. /// @return \c NO by default
  54. - (BOOL)canSelectRow:(NSInteger)row;
  55. /// An action "future" to be triggered when the row is selected, if the row
  56. /// supports being selected as indicated by \c canSelectRow:. Subclasses
  57. /// must implement this in accordance with how they implement \c canSelectRow:
  58. /// if they do not implement \c viewControllerToPushForRow:
  59. /// @return This returns \c nil if no view controller is provided by
  60. /// \c viewControllerToPushForRow: — otherwise it pushes that view controller
  61. /// onto \c host.navigationController
  62. - (nullable void(^)(__kindof UIViewController *host))didSelectRowAction:(NSInteger)row;
  63. /// A view controller to display when the row is selected, if the row
  64. /// supports being selected as indicated by \c canSelectRow:. Subclasses
  65. /// must implement this in accordance with how they implement \c canSelectRow:
  66. /// if they do not implement \c didSelectRowAction:
  67. /// @return \c nil by default
  68. - (nullable UIViewController *)viewControllerToPushForRow:(NSInteger)row;
  69. /// Called when the accessory view's detail button is pressed.
  70. /// @return \c nil by default.
  71. - (nullable void(^)(__kindof UIViewController *host))didPressInfoButtonAction:(NSInteger)row;
  72. #pragma mark - Context Menus
  73. #if FLEX_AT_LEAST_IOS13_SDK
  74. /// By default, this is the title of the row.
  75. /// @return The title of the context menu, if any.
  76. - (nullable NSString *)menuTitleForRow:(NSInteger)row API_AVAILABLE(ios(13.0));
  77. /// Protected, not intended for public use. \c menuTitleForRow:
  78. /// already includes the value returned from this method.
  79. ///
  80. /// By default, this returns \c @"". Subclasses may override to
  81. /// provide a detailed description of the target of the context menu.
  82. - (NSString *)menuSubtitleForRow:(NSInteger)row API_AVAILABLE(ios(13.0));
  83. /// The context menu items, if any. Subclasses may override.
  84. /// By default, only inludes items for \c copyMenuItemsForRow:.
  85. - (nullable NSArray<UIMenuElement *> *)menuItemsForRow:(NSInteger)row sender:(UIViewController *)sender API_AVAILABLE(ios(13.0));
  86. /// Subclasses may override to return a list of copiable items.
  87. ///
  88. /// Every two elements in the list compose a key-value pair, where the key
  89. /// should be a description of what will be copied, and the values should be
  90. /// the strings to copy. Return an empty string as a value to show a disabled action.
  91. - (nullable NSArray<NSString *> *)copyMenuItemsForRow:(NSInteger)row API_AVAILABLE(ios(13.0));
  92. #endif
  93. #pragma mark - Cell Configuration
  94. /// Provide a reuse identifier for the given row. Subclasses should override.
  95. ///
  96. /// Custom reuse identifiers should be specified in \c cellRegistrationMapping.
  97. /// You may return any of the identifiers in \c FLEXTableView.h
  98. /// without including them in the \c cellRegistrationMapping.
  99. /// @return \c kFLEXDefaultCell by default.
  100. - (NSString *)reuseIdentifierForRow:(NSInteger)row;
  101. /// Configure a cell for the given row. Subclasses must override.
  102. - (void)configureCell:(__kindof UITableViewCell *)cell forRow:(NSInteger)row;
  103. #pragma mark - External Convenience
  104. /// For use by whatever view controller uses your section. Not required.
  105. /// @return An optional title.
  106. - (nullable NSString *)titleForRow:(NSInteger)row;
  107. /// For use by whatever view controller uses your section. Not required.
  108. /// @return An optional subtitle.
  109. - (nullable NSString *)subtitleForRow:(NSInteger)row;
  110. @end
  111. NS_ASSUME_NONNULL_END