FLEXSingleRowSection.m 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. //
  2. // FLEXSingleRowSection.m
  3. // FLEX
  4. //
  5. // Created by Tanner Bennett on 9/25/19.
  6. // Copyright © 2019 Flipboard. All rights reserved.
  7. //
  8. #import "FLEXSingleRowSection.h"
  9. #import "FLEXTableView.h"
  10. @interface FLEXSingleRowSection ()
  11. @property (nonatomic, readonly) NSString *reuseIdentifier;
  12. @property (nonatomic, readonly) void (^cellConfiguration)(__kindof UITableViewCell *cell);
  13. @property (nonatomic) NSString *lastTitle;
  14. @property (nonatomic) NSString *lastSubitle;
  15. @end
  16. @implementation FLEXSingleRowSection
  17. @synthesize title = _title;
  18. #pragma mark - Public
  19. + (instancetype)title:(NSString *)title
  20. reuse:(NSString *)reuse
  21. cell:(void (^)(__kindof UITableViewCell *))config {
  22. return [[self alloc] initWithTitle:title reuse:reuse cell:config];
  23. }
  24. - (id)initWithTitle:(NSString *)sectionTitle
  25. reuse:(NSString *)reuseIdentifier
  26. cell:(void (^)(__kindof UITableViewCell *))cellConfiguration {
  27. self = [super init];
  28. if (self) {
  29. _title = sectionTitle;
  30. _reuseIdentifier = reuseIdentifier ?: kFLEXDefaultCell;
  31. _cellConfiguration = cellConfiguration;
  32. }
  33. return self;
  34. }
  35. #pragma mark - Overrides
  36. - (NSInteger)numberOfRows {
  37. if (self.filterMatcher && self.filterText.length) {
  38. return self.filterMatcher(self.filterText) ? 1 : 0;
  39. }
  40. return 1;
  41. }
  42. - (BOOL)canSelectRow:(NSInteger)row {
  43. return self.pushOnSelection || self.selectionAction;
  44. }
  45. - (void (^)(UIViewController *))didSelectRowAction:(NSInteger)row {
  46. return self.selectionAction;
  47. }
  48. - (UIViewController *)viewControllerToPushForRow:(NSInteger)row {
  49. return self.pushOnSelection;
  50. }
  51. - (NSString *)reuseIdentifierForRow:(NSInteger)row {
  52. return self.reuseIdentifier;
  53. }
  54. - (void)configureCell:(__kindof UITableViewCell *)cell forRow:(NSInteger)row {
  55. cell.textLabel.text = nil;
  56. cell.detailTextLabel.text = nil;
  57. cell.accessoryType = UITableViewCellAccessoryNone;
  58. self.cellConfiguration(cell);
  59. self.lastTitle = cell.textLabel.text;
  60. self.lastSubitle = cell.detailTextLabel.text;
  61. }
  62. - (NSString *)titleForRow:(NSInteger)row {
  63. return self.lastTitle;
  64. }
  65. - (NSString *)subtitleForRow:(NSInteger)row {
  66. return self.lastSubitle;
  67. }
  68. @end