FLEXTableContentViewController.m 5.7 KB

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