FLEXTableContentViewController.m 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. //
  2. // PTTableContentViewController.m
  3. // PTDatabaseReader
  4. //
  5. // Created by Peng Tao on 15/11/23.
  6. // Copyright © 2015年 Peng Tao. All rights reserved.
  7. //
  8. #import "FLEXTableContentViewController.h"
  9. #import "FLEXMultiColumnTableView.h"
  10. #import "FLEXWebViewController.h"
  11. #import "FLEXUtility.h"
  12. @interface FLEXTableContentViewController () <
  13. FLEXMultiColumnTableViewDataSource, FLEXMultiColumnTableViewDelegate
  14. >
  15. @property (nonatomic, readonly) NSArray<NSString *> *columns;
  16. @property (nonatomic, copy) NSArray<NSArray *> *rows;
  17. @property (nonatomic) FLEXMultiColumnTableView *multiColumnView;
  18. @end
  19. @implementation FLEXTableContentViewController
  20. + (instancetype)columns:(NSArray<NSString *> *)columnNames
  21. rows:(NSArray<NSArray<NSString *> *> *)rowData {
  22. FLEXTableContentViewController *controller = [self new];
  23. controller->_columns = columnNames;
  24. controller->_rows = rowData;
  25. return controller;
  26. }
  27. - (void)loadView {
  28. [super loadView];
  29. [self.view addSubview:self.multiColumnView];
  30. }
  31. - (void)viewDidLoad {
  32. [super viewDidLoad];
  33. self.edgesForExtendedLayout = UIRectEdgeNone;
  34. [self.multiColumnView reloadData];
  35. }
  36. - (FLEXMultiColumnTableView *)multiColumnView {
  37. if (!_multiColumnView) {
  38. _multiColumnView = [[FLEXMultiColumnTableView alloc]
  39. initWithFrame:FLEXRectSetSize(CGRectZero, self.view.frame.size)
  40. ];
  41. _multiColumnView.dataSource = self;
  42. _multiColumnView.delegate = self;
  43. }
  44. return _multiColumnView;
  45. }
  46. #pragma mark MultiColumnTableView DataSource
  47. - (NSInteger)numberOfColumnsInTableView:(FLEXMultiColumnTableView *)tableView {
  48. return self.columns.count;
  49. }
  50. - (NSInteger)numberOfRowsInTableView:(FLEXMultiColumnTableView *)tableView {
  51. return self.rows.count;
  52. }
  53. - (NSString *)columnTitle:(NSInteger)column {
  54. return self.columns[column];
  55. }
  56. - (NSString *)rowTitle:(NSInteger)row {
  57. return @(row).stringValue;
  58. }
  59. - (NSArray *)contentForRow:(NSInteger)row {
  60. return self.rows[row];
  61. }
  62. - (CGFloat)multiColumnTableView:(FLEXMultiColumnTableView *)tableView
  63. heightForContentCellInRow:(NSInteger)row {
  64. return 40;
  65. }
  66. - (CGFloat)multiColumnTableView:(FLEXMultiColumnTableView *)tableView
  67. widthForContentCellInColumn:(NSInteger)column {
  68. return 120;
  69. }
  70. - (CGFloat)heightForTopHeaderInTableView:(FLEXMultiColumnTableView *)tableView {
  71. return 40;
  72. }
  73. - (CGFloat)widthForLeftHeaderInTableView:(FLEXMultiColumnTableView *)tableView {
  74. NSString *str = [NSString stringWithFormat:@"%lu",(unsigned long)self.rows.count];
  75. NSDictionary *attrs = @{ NSFontAttributeName : [UIFont systemFontOfSize:17.0] };
  76. CGSize size = [str boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, 14)
  77. options:NSStringDrawingUsesLineFragmentOrigin
  78. attributes:attrs context:nil
  79. ].size;
  80. return size.width + 20;
  81. }
  82. #pragma mark MultiColumnTableView Delegate
  83. - (void)multiColumnTableView:(FLEXMultiColumnTableView *)tableView didSelectRow:(NSInteger)row {
  84. NSArray<NSString *> *fields = [self.rows[row] flex_mapped:^id(NSString *field, NSUInteger idx) {
  85. return [NSString stringWithFormat:@"%@:\n%@", self.columns[idx], field];
  86. }];
  87. [FLEXAlert makeAlert:^(FLEXAlert *make) {
  88. make.title([@"Row " stringByAppendingString:@(row).stringValue]);
  89. NSString *message = [fields componentsJoinedByString:@"\n\n"];
  90. make.message(message);
  91. make.button(@"Copy").handler(^(NSArray<NSString *> *strings) {
  92. #if !TARGET_OS_TV
  93. UIPasteboard.generalPasteboard.string = message;
  94. #endif
  95. });
  96. make.button(@"Dismiss").cancelStyle();
  97. } showFrom:self];
  98. }
  99. - (void)multiColumnTableView:(FLEXMultiColumnTableView *)tableView
  100. didSelectHeaderForColumn:(NSInteger)column
  101. sortType:(FLEXTableColumnHeaderSortType)sortType {
  102. NSArray<NSArray *> *sortContentData = [self.rows
  103. sortedArrayUsingComparator:^NSComparisonResult(NSArray *obj1, NSArray *obj2) {
  104. id a = obj1[column], b = obj2[column];
  105. if (a == NSNull.null) {
  106. return NSOrderedAscending;
  107. }
  108. if (b == NSNull.null) {
  109. return NSOrderedDescending;
  110. }
  111. if ([a respondsToSelector:@selector(compare:)] && [b respondsToSelector:@selector(compare:)]) {
  112. return [a compare:b];
  113. }
  114. return NSOrderedSame;
  115. }
  116. ];
  117. if (sortType == FLEXTableColumnHeaderSortTypeDesc) {
  118. sortContentData = sortContentData.reverseObjectEnumerator.allObjects.copy;
  119. }
  120. self.rows = sortContentData;
  121. [self.multiColumnView reloadData];
  122. }
  123. #pragma mark -
  124. #pragma mark About Transition
  125. - (void)willTransitionToTraitCollection:(UITraitCollection *)newCollection
  126. withTransitionCoordinator:(id <UIViewControllerTransitionCoordinator>)coordinator {
  127. [super willTransitionToTraitCollection:newCollection withTransitionCoordinator:coordinator];
  128. [coordinator animateAlongsideTransition:^(id <UIViewControllerTransitionCoordinatorContext> context) {
  129. if (newCollection.verticalSizeClass == UIUserInterfaceSizeClassCompact) {
  130. self.multiColumnView.frame = CGRectMake(0, 32, self.view.frame.size.width, self.view.frame.size.height - 32);
  131. }
  132. else {
  133. self.multiColumnView.frame = CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height - 64);
  134. }
  135. [self.view setNeedsLayout];
  136. } completion:nil];
  137. }
  138. @end