FLEXTableView.m 3.8 KB

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