FLEXTableView.m 3.7 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. - (CGFloat)_heightForHeaderInSection:(NSInteger)section {
  38. CGFloat height = [super _heightForHeaderInSection:section];
  39. if (section == 0) {
  40. NSString *title = [self _titleForHeaderInSection:section];
  41. if (self.tableHeaderView) {
  42. if (!@available(iOS 13, *)) {
  43. return height - self.tableHeaderView.frame.size.height + 8;
  44. }
  45. // On iOS 13, returning an empty title for the table view
  46. // messes with the height of the table view later on.
  47. // We return a space to work around this.
  48. else if ([title isEqualToString:@" "]) {
  49. return height - self.tableHeaderView.frame.size.height + 5;
  50. }
  51. } else {
  52. if (@available(iOS 13, *) && [title isEqualToString:@" "]) {
  53. return 5;
  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. kFLEXKeyValueCell : [FLEXKeyValueTableViewCell class],
  86. kFLEXCodeFontCell : [FLEXCodeFontCell class],
  87. }];
  88. }
  89. return self;
  90. }
  91. #pragma mark - Public
  92. - (void)registerCells:(NSDictionary<NSString*, Class> *)registrationMapping {
  93. [registrationMapping enumerateKeysAndObjectsUsingBlock:^(NSString *identifier, Class cellClass, BOOL *stop) {
  94. [self registerClass:cellClass forCellReuseIdentifier:identifier];
  95. }];
  96. }
  97. @end