FLEXTableViewSection.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. //
  2. // FLEXTableViewSection.h
  3. // FLEX
  4. //
  5. // Created by Tanner on 1/29/20.
  6. // Copyright © 2020 FLEX Team. All rights reserved.
  7. //
  8. #import <UIKit/UIKit.h>
  9. #import "FLEXMacros.h"
  10. #import "NSArray+FLEX.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. @private
  24. __weak UITableView *_tableView;
  25. NSInteger _sectionIndex;
  26. }
  27. #pragma mark - Data
  28. /// A title to be displayed for the custom section.
  29. /// Subclasses may override or use the \c _title ivar.
  30. @property (nonatomic, readonly, nullable, copy) NSString *title;
  31. /// The number of rows in this section. Subclasses must override.
  32. /// This should not change until \c filterText is changed or \c reloadData is called.
  33. @property (nonatomic, readonly) NSInteger numberOfRows;
  34. /// A map of reuse identifiers to \c UITableViewCell (sub)class objects.
  35. /// Subclasses \e may override this as necessary, but are not required to.
  36. /// See \c FLEXTableView.h for more information.
  37. /// @return nil by default.
  38. @property (nonatomic, readonly, nullable) NSDictionary<NSString *, Class> *cellRegistrationMapping;
  39. /// The section should filter itself based on the contents of this property
  40. /// as it is set. If it is set to nil or an empty string, it should not filter.
  41. /// Subclasses should override or observe this property and react to changes.
  42. ///
  43. /// It is common practice to use two arrays for the underlying model:
  44. /// One to hold all rows, and one to hold unfiltered rows. When \c setFilterText:
  45. /// is called, call \c super to store the new value, and re-filter your model accordingly.
  46. @property (nonatomic, nullable) NSString *filterText;
  47. /// Provides an avenue for the section to refresh data or change the number of rows.
  48. ///
  49. /// This is called before reloading the table view itself. If your section pulls data
  50. /// from an external data source, this is a good place to refresh that data entirely.
  51. /// If your section does not, then it might be simpler for you to just override
  52. /// \c setFilterText: to call \c super and call \c reloadData.
  53. - (void)reloadData;
  54. /// Like \c reloadData, but optionally reloads the table view section
  55. /// associated with this section object, if any. Do not override.
  56. /// Do not call outside of the main thread.
  57. - (void)reloadData:(BOOL)updateTable;
  58. /// Provide a table view and section index to allow the section to efficiently reload
  59. /// its own section of the table when something changes it. The table reference is
  60. /// held weakly, and subclasses cannot access it or the index. Call this method again
  61. /// if the section numbers have changed since you last called it.
  62. - (void)setTable:(UITableView *)tableView section:(NSInteger)index;
  63. #pragma mark - Row Selection
  64. /// Whether the given row should be selectable, such as if tapping the cell
  65. /// should take the user to a new screen or trigger an action.
  66. /// Subclasses \e may override this as necessary, but are not required to.
  67. /// @return \c NO by default
  68. - (BOOL)canSelectRow:(NSInteger)row;
  69. /// An action "future" to be triggered when the row is selected, if the row
  70. /// supports being selected as indicated by \c canSelectRow:. Subclasses
  71. /// must implement this in accordance with how they implement \c canSelectRow:
  72. /// if they do not implement \c viewControllerToPushForRow:
  73. /// @return This returns \c nil if no view controller is provided by
  74. /// \c viewControllerToPushForRow: — otherwise it pushes that view controller
  75. /// onto \c host.navigationController
  76. - (nullable void(^)(__kindof UIViewController *host))didSelectRowAction:(NSInteger)row;
  77. /// A view controller to display when the row is selected, if the row
  78. /// supports being selected as indicated by \c canSelectRow:. Subclasses
  79. /// must implement this in accordance with how they implement \c canSelectRow:
  80. /// if they do not implement \c didSelectRowAction:
  81. /// @return \c nil by default
  82. - (nullable UIViewController *)viewControllerToPushForRow:(NSInteger)row;
  83. /// Called when the accessory view's detail button is pressed.
  84. /// @return \c nil by default.
  85. - (nullable void(^)(__kindof UIViewController *host))didPressInfoButtonAction:(NSInteger)row;
  86. #pragma mark - Context Menus
  87. #if FLEX_AT_LEAST_IOS13_SDK
  88. /// By default, this is the title of the row.
  89. /// @return The title of the context menu, if any.
  90. - (nullable NSString *)menuTitleForRow:(NSInteger)row API_AVAILABLE(ios(13.0));
  91. /// Protected, not intended for public use. \c menuTitleForRow:
  92. /// already includes the value returned from this method.
  93. ///
  94. /// By default, this returns \c @"". Subclasses may override to
  95. /// provide a detailed description of the target of the context menu.
  96. - (NSString *)menuSubtitleForRow:(NSInteger)row API_AVAILABLE(ios(13.0));
  97. /// The context menu items, if any. Subclasses may override.
  98. /// By default, only inludes items for \c copyMenuItemsForRow:.
  99. - (nullable NSArray<UIMenuElement *> *)menuItemsForRow:(NSInteger)row sender:(UIViewController *)sender API_AVAILABLE(ios(13.0));
  100. /// Subclasses may override to return a list of copiable items.
  101. ///
  102. /// Every two elements in the list compose a key-value pair, where the key
  103. /// should be a description of what will be copied, and the values should be
  104. /// the strings to copy. Return an empty string as a value to show a disabled action.
  105. - (nullable NSArray<NSString *> *)copyMenuItemsForRow:(NSInteger)row API_AVAILABLE(ios(13.0));
  106. #endif
  107. #pragma mark - Cell Configuration
  108. /// Provide a reuse identifier for the given row. Subclasses should override.
  109. ///
  110. /// Custom reuse identifiers should be specified in \c cellRegistrationMapping.
  111. /// You may return any of the identifiers in \c FLEXTableView.h
  112. /// without including them in the \c cellRegistrationMapping.
  113. /// @return \c kFLEXDefaultCell by default.
  114. - (NSString *)reuseIdentifierForRow:(NSInteger)row;
  115. /// Configure a cell for the given row. Subclasses must override.
  116. - (void)configureCell:(__kindof UITableViewCell *)cell forRow:(NSInteger)row;
  117. #pragma mark - External Convenience
  118. /// For use by whatever view controller uses your section. Not required.
  119. /// @return An optional title.
  120. - (nullable NSString *)titleForRow:(NSInteger)row;
  121. /// For use by whatever view controller uses your section. Not required.
  122. /// @return An optional subtitle.
  123. - (nullable NSString *)subtitleForRow:(NSInteger)row;
  124. @end
  125. NS_ASSUME_NONNULL_END