FLEXTableContentViewController.m 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. @interface FLEXTableContentViewController ()<FLEXMultiColumnTableViewDataSource, FLEXMultiColumnTableViewDelegate>
  12. @property (nonatomic) FLEXMultiColumnTableView *multiColumnView;
  13. @end
  14. @implementation FLEXTableContentViewController
  15. - (void)viewDidLoad {
  16. [super viewDidLoad];
  17. self.edgesForExtendedLayout = UIRectEdgeNone;
  18. [self.view addSubview:self.multiColumnView];
  19. }
  20. - (void)viewWillAppear:(BOOL)animated {
  21. [super viewWillAppear:animated];
  22. [self.multiColumnView reloadData];
  23. }
  24. #pragma mark -
  25. #pragma mark init SubView
  26. - (FLEXMultiColumnTableView *)multiColumnView {
  27. if (!_multiColumnView) {
  28. _multiColumnView = [[FLEXMultiColumnTableView alloc] initWithFrame:
  29. CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
  30. _multiColumnView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleTopMargin;
  31. _multiColumnView.backgroundColor = UIColor.whiteColor;
  32. _multiColumnView.dataSource = self;
  33. _multiColumnView.delegate = self;
  34. }
  35. return _multiColumnView;
  36. }
  37. #pragma mark MultiColumnTableView DataSource
  38. - (NSInteger)numberOfColumnsInTableView:(FLEXMultiColumnTableView *)tableView {
  39. return self.columnsArray.count;
  40. }
  41. - (NSInteger)numberOfRowsInTableView:(FLEXMultiColumnTableView *)tableView {
  42. return self.contentsArray.count;
  43. }
  44. - (NSString *)columnNameInColumn:(NSInteger)column {
  45. return self.columnsArray[column];
  46. }
  47. - (NSString *)rowNameInRow:(NSInteger)row {
  48. return [NSString stringWithFormat:@"%ld",(long)row];
  49. }
  50. - (NSString *)contentAtColumn:(NSInteger)column row:(NSInteger)row {
  51. if (self.contentsArray.count > row) {
  52. NSDictionary<NSString *, id> *dic = self.contentsArray[row];
  53. if (self.contentsArray.count > column) {
  54. return [NSString stringWithFormat:@"%@",[dic objectForKey:self.columnsArray[column]]];
  55. }
  56. }
  57. return @"";
  58. }
  59. - (NSArray *)contentAtRow:(NSInteger)row {
  60. NSMutableArray *result = [NSMutableArray array];
  61. if (self.contentsArray.count > row) {
  62. NSDictionary<NSString *, id> *dic = self.contentsArray[row];
  63. for (int i = 0; i < self.columnsArray.count; i ++) {
  64. [result addObject:dic[self.columnsArray[i]]];
  65. }
  66. return result;
  67. }
  68. return nil;
  69. }
  70. - (CGFloat)multiColumnTableView:(FLEXMultiColumnTableView *)tableView
  71. heightForContentCellInRow:(NSInteger)row {
  72. return 40;
  73. }
  74. - (CGFloat)multiColumnTableView:(FLEXMultiColumnTableView *)tableView
  75. widthForContentCellInColumn:(NSInteger)column {
  76. return 120;
  77. }
  78. - (CGFloat)heightForTopHeaderInTableView:(FLEXMultiColumnTableView *)tableView {
  79. return 40;
  80. }
  81. - (CGFloat)widthForLeftHeaderInTableView:(FLEXMultiColumnTableView *)tableView {
  82. NSString *str = [NSString stringWithFormat:@"%lu",(unsigned long)self.contentsArray.count];
  83. NSDictionary<NSString *, id> *attrs = @{@"NSFontAttributeName":[UIFont systemFontOfSize:17.0]};
  84. CGSize size = [str boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, 14)
  85. options:NSStringDrawingUsesLineFragmentOrigin
  86. attributes:attrs context:nil].size;
  87. return size.width + 20;
  88. }
  89. #pragma mark -
  90. #pragma mark MultiColumnTableView Delegate
  91. - (void)multiColumnTableView:(FLEXMultiColumnTableView *)tableView didTapLabelWithText:(NSString *)text {
  92. FLEXWebViewController * detailViewController = [[FLEXWebViewController alloc] initWithText:text];
  93. [self.navigationController pushViewController:detailViewController animated:YES];
  94. }
  95. - (void)multiColumnTableView:(FLEXMultiColumnTableView *)tableView didTapHeaderWithText:(NSString *)text sortType:(FLEXTableColumnHeaderSortType)sortType {
  96. NSArray<NSDictionary<NSString *, id> *> *sortContentData = [self.contentsArray sortedArrayUsingComparator:^NSComparisonResult(NSDictionary<NSString *, id> * obj1, NSDictionary<NSString *, id> * obj2) {
  97. if ([obj1 objectForKey:text] == [NSNull null]) {
  98. return NSOrderedAscending;
  99. }
  100. if ([obj2 objectForKey:text] == [NSNull null]) {
  101. return NSOrderedDescending;
  102. }
  103. if (![[obj1 objectForKey:text] respondsToSelector:@selector(compare:)] && ![[obj2 objectForKey:text] respondsToSelector:@selector(compare:)]) {
  104. return NSOrderedSame;
  105. }
  106. NSComparisonResult result = [[obj1 objectForKey:text] compare:[obj2 objectForKey:text]];
  107. return result;
  108. }];
  109. if (sortType == FLEXTableColumnHeaderSortTypeDesc) {
  110. NSEnumerator *contentReverseEnumerator = sortContentData.reverseObjectEnumerator;
  111. sortContentData = [NSArray arrayWithArray:contentReverseEnumerator.allObjects];
  112. }
  113. self.contentsArray = sortContentData;
  114. [self.multiColumnView reloadData];
  115. }
  116. #pragma mark -
  117. #pragma mark About Transition
  118. - (void)willTransitionToTraitCollection:(UITraitCollection *)newCollection
  119. withTransitionCoordinator:(id <UIViewControllerTransitionCoordinator>)coordinator {
  120. [super willTransitionToTraitCollection:newCollection
  121. withTransitionCoordinator:coordinator];
  122. [coordinator animateAlongsideTransition:^(id <UIViewControllerTransitionCoordinatorContext> context) {
  123. if (newCollection.verticalSizeClass == UIUserInterfaceSizeClassCompact) {
  124. self->_multiColumnView.frame = CGRectMake(0, 32, self.view.frame.size.width, self.view.frame.size.height - 32);
  125. }
  126. else {
  127. self->_multiColumnView.frame = CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height - 64);
  128. }
  129. [self.view setNeedsLayout];
  130. } completion:nil];
  131. }
  132. @end