FLEXTableViewSection.m 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. //
  2. // FLEXTableViewSection.m
  3. // FLEX
  4. //
  5. // Created by Tanner on 1/29/20.
  6. // Copyright © 2020 FLEX Team. All rights reserved.
  7. //
  8. #import "FLEXTableViewSection.h"
  9. #import "FLEXTableView.h"
  10. #import "FLEXUtility.h"
  11. #import "UIMenu+FLEX.h"
  12. #pragma clang diagnostic push
  13. #pragma clang diagnostic ignored "-Wincomplete-implementation"
  14. @implementation FLEXTableViewSection
  15. - (NSInteger)numberOfRows {
  16. return 0;
  17. }
  18. - (void)reloadData { }
  19. - (void)reloadData:(BOOL)updateTable {
  20. [self reloadData];
  21. if (updateTable) {
  22. NSIndexSet *index = [NSIndexSet indexSetWithIndex:_sectionIndex];
  23. [_tableView reloadSections:index withRowAnimation:UITableViewRowAnimationNone];
  24. }
  25. }
  26. - (void)setTable:(UITableView *)tableView section:(NSInteger)index {
  27. _tableView = tableView;
  28. _sectionIndex = index;
  29. }
  30. - (NSDictionary<NSString *,Class> *)cellRegistrationMapping {
  31. return nil;
  32. }
  33. - (BOOL)canSelectRow:(NSInteger)row { return NO; }
  34. - (void (^)(__kindof UIViewController *))didSelectRowAction:(NSInteger)row {
  35. UIViewController *toPush = [self viewControllerToPushForRow:row];
  36. if (toPush) {
  37. return ^(UIViewController *host) {
  38. [host.navigationController pushViewController:toPush animated:YES];
  39. };
  40. }
  41. return nil;
  42. }
  43. - (UIViewController *)viewControllerToPushForRow:(NSInteger)row {
  44. return nil;
  45. }
  46. - (void (^)(__kindof UIViewController *))didPressInfoButtonAction:(NSInteger)row {
  47. return nil;
  48. }
  49. - (NSString *)reuseIdentifierForRow:(NSInteger)row {
  50. return kFLEXDefaultCell;
  51. }
  52. #if FLEX_AT_LEAST_IOS13_SDK
  53. - (NSString *)menuTitleForRow:(NSInteger)row {
  54. NSString *title = [self titleForRow:row];
  55. NSString *subtitle = [self menuSubtitleForRow:row];
  56. if (subtitle.length) {
  57. return [NSString stringWithFormat:@"%@\n\n%@", title, subtitle];
  58. }
  59. return title;
  60. }
  61. - (NSString *)menuSubtitleForRow:(NSInteger)row {
  62. return @"";
  63. }
  64. - (NSArray<UIMenuElement *> *)menuItemsForRow:(NSInteger)row sender:(UIViewController *)sender API_AVAILABLE(ios(13)) {
  65. NSArray<NSString *> *copyItems = [self copyMenuItemsForRow:row];
  66. NSAssert(copyItems.count % 2 == 0, @"copyMenuItemsForRow: should return an even list");
  67. if (copyItems.count) {
  68. NSInteger numberOfActions = copyItems.count / 2;
  69. BOOL collapseMenu = numberOfActions > 4;
  70. UIImage *copyIcon = [UIImage systemImageNamed:@"doc.on.doc"];
  71. NSMutableArray *actions = [NSMutableArray new];
  72. for (NSInteger i = 0; i < copyItems.count; i += 2) {
  73. NSString *key = copyItems[i], *value = copyItems[i+1];
  74. NSString *title = collapseMenu ? key : [@"Copy " stringByAppendingString:key];
  75. UIAction *copy = [UIAction
  76. actionWithTitle:title
  77. image:copyIcon
  78. identifier:nil
  79. handler:^(__kindof UIAction *action) {
  80. #if !TARGET_OS_TV
  81. UIPasteboard.generalPasteboard.string = value;
  82. #endif
  83. }
  84. ];
  85. if (!value.length) {
  86. copy.attributes = UIMenuElementAttributesDisabled;
  87. }
  88. [actions addObject:copy];
  89. }
  90. UIMenu *copyMenu = [UIMenu
  91. flex_inlineMenuWithTitle:@"Copy…"
  92. image:copyIcon
  93. children:actions
  94. ];
  95. if (collapseMenu) {
  96. return @[[copyMenu flex_collapsed]];
  97. } else {
  98. return @[copyMenu];
  99. }
  100. }
  101. return @[];
  102. }
  103. #endif
  104. - (NSArray<NSString *> *)copyMenuItemsForRow:(NSInteger)row {
  105. return nil;
  106. }
  107. - (NSString *)titleForRow:(NSInteger)row { return nil; }
  108. - (NSString *)subtitleForRow:(NSInteger)row { return nil; }
  109. @end
  110. #pragma clang diagnostic pop