You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
2.9 KiB

5 years ago
5 years ago
5 years ago
5 years ago
  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. FLEXTableViewCellReuseIdentifier const kFLEXDefaultCell = @"kFLEXDefaultCell";
  13. FLEXTableViewCellReuseIdentifier const kFLEXDetailCell = @"kFLEXDetailCell";
  14. FLEXTableViewCellReuseIdentifier const kFLEXMultilineCell = @"kFLEXMultilineCell";
  15. FLEXTableViewCellReuseIdentifier const kFLEXMultilineDetailCell = @"kFLEXMultilineDetailCell";
  16. #pragma mark Private
  17. @interface UITableView (Private)
  18. - (CGFloat)_heightForHeaderInSection:(NSInteger)section;
  19. - (NSString *)_titleForHeaderInSection:(NSInteger)section;
  20. @end
  21. @implementation FLEXTableView
  22. + (instancetype)flexDefaultTableView {
  23. #if FLEX_AT_LEAST_IOS13_SDK
  24. if (@available(iOS 13.0, *)) {
  25. return [[self alloc] initWithFrame:CGRectZero style:UITableViewStyleInsetGrouped];
  26. } else {
  27. return [[self alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
  28. }
  29. #else
  30. return [[self alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
  31. #endif
  32. }
  33. - (CGFloat)_heightForHeaderInSection:(NSInteger)section {
  34. CGFloat height = [super _heightForHeaderInSection:section];
  35. if (section == 0 && self.tableHeaderView) {
  36. NSString *title = [self _titleForHeaderInSection:section];
  37. if (!@available(iOS 13, *)) {
  38. return height - self.tableHeaderView.frame.size.height + 8;
  39. } else if ([title isEqualToString:@" "]) {
  40. return height - self.tableHeaderView.frame.size.height + 5;
  41. }
  42. }
  43. return height;
  44. }
  45. #pragma mark - Initialization
  46. + (id)groupedTableView {
  47. #if FLEX_AT_LEAST_IOS13_SDK
  48. if (@available(iOS 13.0, *)) {
  49. return [[self alloc] initWithFrame:CGRectZero style:UITableViewStyleInsetGrouped];
  50. } else {
  51. return [[self alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
  52. }
  53. #else
  54. return [[self alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
  55. #endif
  56. }
  57. + (id)plainTableView {
  58. return [[self alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
  59. }
  60. - (id)initWithFrame:(CGRect)frame style:(UITableViewStyle)style {
  61. self = [super initWithFrame:frame style:style];
  62. if (self) {
  63. [self registerCells:@{
  64. kFLEXDefaultCell : [FLEXTableViewCell class],
  65. kFLEXDetailCell : [FLEXSubtitleTableViewCell class],
  66. kFLEXMultilineCell : [FLEXMultilineTableViewCell class],
  67. kFLEXMultilineDetailCell : [FLEXMultilineDetailTableViewCell class]
  68. }];
  69. }
  70. return self;
  71. }
  72. #pragma mark - Public
  73. - (void)registerCells:(NSDictionary<NSString*, Class> *)registrationMapping {
  74. [registrationMapping enumerateKeysAndObjectsUsingBlock:^(NSString *identifier, Class cellClass, BOOL *stop) {
  75. [self registerClass:cellClass forCellReuseIdentifier:identifier];
  76. }];
  77. }
  78. @end