FLEXTableViewSection.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. #pragma mark - Data
  19. /// A title to be displayed for the custom section. Subclasses may override.
  20. @property (nonatomic, readonly, nullable) NSString *title;
  21. /// The number of rows in this section.
  22. /// This should not change until \c filterText is changed or \c reloadData is called.
  23. @property (nonatomic, readonly) NSInteger numberOfRows;
  24. /// A map of reuse identifiers to \c UITableViewCell (sub)class objects.
  25. /// Subclasses \e may override this as necessary, but are not required to.
  26. /// See \c FLEXTableView.h for more information.
  27. /// @return nil by default.
  28. @property (nonatomic, readonly, nullable) NSDictionary<NSString *, Class> *cellRegistrationMapping;
  29. /// The section should filter itself based on the contents of this property
  30. /// as it is set. If it is set to nil or an empty string, it should not filter.
  31. /// Subclasses should override or observe this property and react to changes.
  32. @property (nonatomic, nullable) NSString *filterText;
  33. /// Provides an avenue for the section to change the number of rows.
  34. /// This is called before reloading the table view itself.
  35. - (void)reloadData;
  36. #pragma mark - Row Selection
  37. /// Whether the given row should be selectable, such as if tapping the cell
  38. /// should take the user to a new screen or trigger an action.
  39. /// Subclasses \e may override this as necessary, but are not required to.
  40. /// @return \c NO by default
  41. - (BOOL)canSelectRow:(NSInteger)row;
  42. /// An action "future" to be triggered when the row is selected, if the row
  43. /// supports being selected as indicated by \c canSelectRow:. Subclasses
  44. /// must implement this in accordance with how they implement \c canSelectRow:
  45. /// if they do not implement \c viewControllerToPushForRow:
  46. /// @return This returns \c nil if no view controller is provided by
  47. /// \c viewControllerToPushForRow: — otherwise it pushes that view controller
  48. /// onto \c host.navigationController
  49. - (nullable void(^)(UIViewController *host))didSelectRowAction:(NSInteger)row;
  50. /// A view controller to display when the row is selected, if the row
  51. /// supports being selected as indicated by \c canSelectRow:. Subclasses
  52. /// must implement this in accordance with how they implement \c canSelectRow:
  53. /// if they do not implement \c didSelectRowAction:
  54. /// @return \c nil by default
  55. - (nullable UIViewController *)viewControllerToPushForRow:(NSInteger)row;
  56. /// Called when the accessory view's detail button is pressed.
  57. /// @return \c nil by default.
  58. - (nullable void(^)(UIViewController *host))didPressInfoButtonAction:(NSInteger)row;
  59. #pragma mark - Context Menus
  60. #if FLEX_AT_LEAST_IOS13_SDK
  61. /// By default, this is the title of the row.
  62. /// @return The title of the context menu, if any.
  63. - (nullable NSString *)menuTitleForRow:(NSInteger)row API_AVAILABLE(ios(13.0));
  64. /// Protected, not intended for public use. \c menuTitleForRow:
  65. /// already includes the value returned from this method.
  66. ///
  67. /// By default, this returns \c @"". Subclasses may override to
  68. /// provide a detailed description of the target of the context menu.
  69. - (NSString *)menuSubtitleForRow:(NSInteger)row API_AVAILABLE(ios(13.0));
  70. /// The context menu items, if any. Subclasses may override.
  71. /// By default, only inludes items for \c copyMenuItemsForRow:.
  72. - (nullable NSArray<UIMenuElement *> *)menuItemsForRow:(NSInteger)row sender:(UIViewController *)sender API_AVAILABLE(ios(13.0));
  73. /// Subclasses may override to return a list of copiable items.
  74. ///
  75. /// Every two elements in the list compose a key-value pair, where the key
  76. /// should be a description of what will be copied, and the values should be
  77. /// the strings to copy. Return an empty string as a value to show a disabled action.
  78. - (nullable NSArray<NSString *> *)copyMenuItemsForRow:(NSInteger)row API_AVAILABLE(ios(13.0));
  79. #endif
  80. #pragma mark - Cell Configuration
  81. /// Provide a reuse identifier for the given row. Subclasses should override.
  82. ///
  83. /// Custom reuse identifiers should be specified in \c cellRegistrationMapping.
  84. /// You may return any of the identifiers in \c FLEXTableView.h
  85. /// without including them in the \c cellRegistrationMapping.
  86. /// @return \c kFLEXDefaultCell by default.
  87. - (NSString *)reuseIdentifierForRow:(NSInteger)row;
  88. /// Configure a cell for the given row. Subclasses must override.
  89. - (void)configureCell:(__kindof UITableViewCell *)cell forRow:(NSInteger)row;
  90. #pragma mark - External Convenience
  91. /// For use by whatever view controller uses your section. Not required.
  92. /// @return An optional title.
  93. - (nullable NSString *)titleForRow:(NSInteger)row;
  94. /// For use by whatever view controller uses your section. Not required.
  95. /// @return An optional subtitle.
  96. - (nullable NSString *)subtitleForRow:(NSInteger)row;
  97. @end
  98. NS_ASSUME_NONNULL_END