FLEXTableView.m 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. - (CGFloat)_heightForHeaderInSection:(NSInteger)section {
  36. CGFloat height = [super _heightForHeaderInSection:section];
  37. if (section == 0 && self.tableHeaderView) {
  38. NSString *title = [self _titleForHeaderInSection:section];
  39. if (!@available(iOS 13, *)) {
  40. return height - self.tableHeaderView.frame.size.height + 8;
  41. } else if ([title isEqualToString:@" "]) {
  42. return height - self.tableHeaderView.frame.size.height + 5;
  43. }
  44. }
  45. return height;
  46. }
  47. #pragma mark - Initialization
  48. + (id)groupedTableView {
  49. #if FLEX_AT_LEAST_IOS13_SDK
  50. if (@available(iOS 13.0, *)) {
  51. return [[self alloc] initWithFrame:CGRectZero style:UITableViewStyleInsetGrouped];
  52. } else {
  53. return [[self alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
  54. }
  55. #else
  56. return [[self alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
  57. #endif
  58. }
  59. + (id)plainTableView {
  60. return [[self alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
  61. }
  62. + (id)style:(UITableViewStyle)style {
  63. return [[self alloc] initWithFrame:CGRectZero style:style];
  64. }
  65. - (id)initWithFrame:(CGRect)frame style:(UITableViewStyle)style {
  66. self = [super initWithFrame:frame style:style];
  67. if (self) {
  68. [self registerCells:@{
  69. kFLEXDefaultCell : [FLEXTableViewCell class],
  70. kFLEXDetailCell : [FLEXSubtitleTableViewCell class],
  71. kFLEXMultilineCell : [FLEXMultilineTableViewCell class],
  72. kFLEXMultilineDetailCell : [FLEXMultilineDetailTableViewCell class],
  73. kFLEXCodeFontCell : [FLEXCodeFontCell class],
  74. }];
  75. }
  76. return self;
  77. }
  78. #pragma mark - Public
  79. - (void)registerCells:(NSDictionary<NSString*, Class> *)registrationMapping {
  80. [registrationMapping enumerateKeysAndObjectsUsingBlock:^(NSString *identifier, Class cellClass, BOOL *stop) {
  81. [self registerClass:cellClass forCellReuseIdentifier:identifier];
  82. }];
  83. }
  84. @end