FLEXTableView.m 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. //
  2. // FLEXTableView.m
  3. // FLEX
  4. //
  5. // Created by Tanner on 4/17/19.
  6. // Copyright © 2019 Flipboard. All rights reserved.
  7. //
  8. #import "FLEXTableView.h"
  9. #import "FLEXUtility.h"
  10. #import "FLEXSubtitleTableViewCell.h"
  11. #import "FLEXMultilineTableViewCell.h"
  12. #import "FLEXCodeFontCell.h"
  13. FLEXTableViewCellReuseIdentifier const kFLEXDefaultCell = @"kFLEXDefaultCell";
  14. FLEXTableViewCellReuseIdentifier const kFLEXDetailCell = @"kFLEXDetailCell";
  15. FLEXTableViewCellReuseIdentifier const kFLEXMultilineCell = @"kFLEXMultilineCell";
  16. FLEXTableViewCellReuseIdentifier const kFLEXMultilineDetailCell = @"kFLEXMultilineDetailCell";
  17. FLEXTableViewCellReuseIdentifier const kFLEXCodeFontCell = @"kFLEXCodeFontCell";
  18. #pragma mark Private
  19. @interface UITableView (Private)
  20. - (CGFloat)_heightForHeaderInSection:(NSInteger)section;
  21. - (NSString *)_titleForHeaderInSection:(NSInteger)section;
  22. @end
  23. @implementation FLEXTableView
  24. + (instancetype)flexDefaultTableView {
  25. #if FLEX_AT_LEAST_IOS13_SDK
  26. if (@available(iOS 13.0, *)) {
  27. return [[self alloc] initWithFrame:CGRectZero style:UITableViewStyleInsetGrouped];
  28. } else {
  29. return [[self alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
  30. }
  31. #else
  32. return [[self alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
  33. #endif
  34. }
  35. //
  36. //- (CGFloat)_heightForHeaderInSection:(NSInteger)section {
  37. // CGFloat height = [super _heightForHeaderInSection:section];
  38. // if (section == 0) {
  39. // NSString *title = [self _titleForHeaderInSection:section];
  40. // if (self.tableHeaderView) {
  41. // if (!@available(iOS 13, *)) {
  42. // return height - self.tableHeaderView.frame.size.height + 8;
  43. // }
  44. // // On iOS 13, returning an empty title for the table view
  45. // // messes with the height of the table view later on.
  46. // // We return a space to work around this.
  47. // else if ([title isEqualToString:@" "]) {
  48. // return height - self.tableHeaderView.frame.size.height + 5;
  49. // }
  50. // } else {
  51. // if (@available(iOS 13, *) && [title isEqualToString:@" "]) {
  52. // return 5;
  53. // }
  54. // }
  55. // }
  56. //
  57. // return height;
  58. //}
  59. #pragma mark - Initialization
  60. + (id)groupedTableView {
  61. #if FLEX_AT_LEAST_IOS13_SDK
  62. if (@available(iOS 13.0, *)) {
  63. return [[self alloc] initWithFrame:CGRectZero style:UITableViewStyleInsetGrouped];
  64. } else {
  65. return [[self alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
  66. }
  67. #else
  68. return [[self alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
  69. #endif
  70. }
  71. + (id)plainTableView {
  72. return [[self alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
  73. }
  74. + (id)style:(UITableViewStyle)style {
  75. return [[self alloc] initWithFrame:CGRectZero style:style];
  76. }
  77. - (id)initWithFrame:(CGRect)frame style:(UITableViewStyle)style {
  78. self = [super initWithFrame:frame style:style];
  79. if (self) {
  80. [self registerCells:@{
  81. kFLEXDefaultCell : [FLEXTableViewCell class],
  82. kFLEXDetailCell : [FLEXSubtitleTableViewCell class],
  83. kFLEXMultilineCell : [FLEXMultilineTableViewCell class],
  84. kFLEXMultilineDetailCell : [FLEXMultilineDetailTableViewCell class],
  85. kFLEXCodeFontCell : [FLEXCodeFontCell class],
  86. }];
  87. }
  88. return self;
  89. }
  90. #pragma mark - Public
  91. - (void)registerCells:(NSDictionary<NSString*, Class> *)registrationMapping {
  92. [registrationMapping enumerateKeysAndObjectsUsingBlock:^(NSString *identifier, Class cellClass, BOOL *stop) {
  93. [self registerClass:cellClass forCellReuseIdentifier:identifier];
  94. }];
  95. }
  96. @end