FLEXTableViewSection.h 5.9 KB

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