123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- //
- // PTTableContentViewController.m
- // PTDatabaseReader
- //
- // Created by Peng Tao on 15/11/23.
- // Copyright © 2015年 Peng Tao. All rights reserved.
- //
- #import "FLEXTableContentViewController.h"
- #import "FLEXMultiColumnTableView.h"
- #import "FLEXWebViewController.h"
- #import "FLEXUtility.h"
- @interface FLEXTableContentViewController () <
- FLEXMultiColumnTableViewDataSource, FLEXMultiColumnTableViewDelegate
- >
- @property (nonatomic, readonly) NSArray<NSString *> *columns;
- @property (nonatomic, copy) NSArray<NSArray *> *rows;
- @property (nonatomic) FLEXMultiColumnTableView *multiColumnView;
- @end
- @implementation FLEXTableContentViewController
- + (instancetype)columns:(NSArray<NSString *> *)columnNames
- rows:(NSArray<NSArray<NSString *> *> *)rowData {
- FLEXTableContentViewController *controller = [self new];
- controller->_columns = columnNames;
- controller->_rows = rowData;
- return controller;
- }
- - (void)loadView {
- [super loadView];
-
- [self.view addSubview:self.multiColumnView];
- }
- - (void)viewDidLoad {
- [super viewDidLoad];
-
- self.edgesForExtendedLayout = UIRectEdgeNone;
- [self.multiColumnView reloadData];
- }
- - (FLEXMultiColumnTableView *)multiColumnView {
- if (!_multiColumnView) {
- _multiColumnView = [[FLEXMultiColumnTableView alloc]
- initWithFrame:FLEXRectSetSize(CGRectZero, self.view.frame.size)
- ];
-
- _multiColumnView.dataSource = self;
- _multiColumnView.delegate = self;
- }
-
- return _multiColumnView;
- }
- #pragma mark MultiColumnTableView DataSource
- - (NSInteger)numberOfColumnsInTableView:(FLEXMultiColumnTableView *)tableView {
- return self.columns.count;
- }
- - (NSInteger)numberOfRowsInTableView:(FLEXMultiColumnTableView *)tableView {
- return self.rows.count;
- }
- - (NSString *)columnTitle:(NSInteger)column {
- return self.columns[column];
- }
- - (NSString *)rowTitle:(NSInteger)row {
- return @(row).stringValue;
- }
- - (NSArray *)contentForRow:(NSInteger)row {
- return self.rows[row];
- }
- - (CGFloat)multiColumnTableView:(FLEXMultiColumnTableView *)tableView
- heightForContentCellInRow:(NSInteger)row {
- return 40;
- }
- - (CGFloat)multiColumnTableView:(FLEXMultiColumnTableView *)tableView
- widthForContentCellInColumn:(NSInteger)column {
- return 120;
- }
- - (CGFloat)heightForTopHeaderInTableView:(FLEXMultiColumnTableView *)tableView {
- return 40;
- }
- - (CGFloat)widthForLeftHeaderInTableView:(FLEXMultiColumnTableView *)tableView {
- NSString *str = [NSString stringWithFormat:@"%lu",(unsigned long)self.rows.count];
- NSDictionary *attrs = @{ NSFontAttributeName : [UIFont systemFontOfSize:17.0] };
- CGSize size = [str boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, 14)
- options:NSStringDrawingUsesLineFragmentOrigin
- attributes:attrs context:nil
- ].size;
-
- return size.width + 20;
- }
- #pragma mark MultiColumnTableView Delegate
- - (void)multiColumnTableView:(FLEXMultiColumnTableView *)tableView didSelectRow:(NSInteger)row {
- NSArray<NSString *> *fields = [self.rows[row] flex_mapped:^id(NSString *field, NSUInteger idx) {
- return [NSString stringWithFormat:@"%@:\n%@", self.columns[idx], field];
- }];
-
- [FLEXAlert makeAlert:^(FLEXAlert *make) {
- make.title([@"Row " stringByAppendingString:@(row).stringValue]);
- NSString *message = [fields componentsJoinedByString:@"\n\n"];
- make.message(message);
- make.button(@"Copy").handler(^(NSArray<NSString *> *strings) {
- #if !TARGET_OS_TV
- UIPasteboard.generalPasteboard.string = message;
- #endif
- });
- make.button(@"Dismiss").cancelStyle();
- } showFrom:self];
- }
- - (void)multiColumnTableView:(FLEXMultiColumnTableView *)tableView
- didSelectHeaderForColumn:(NSInteger)column
- sortType:(FLEXTableColumnHeaderSortType)sortType {
-
- NSArray<NSArray *> *sortContentData = [self.rows
- sortedArrayUsingComparator:^NSComparisonResult(NSArray *obj1, NSArray *obj2) {
- id a = obj1[column], b = obj2[column];
- if (a == NSNull.null) {
- return NSOrderedAscending;
- }
- if (b == NSNull.null) {
- return NSOrderedDescending;
- }
-
- if ([a respondsToSelector:@selector(compare:)] && [b respondsToSelector:@selector(compare:)]) {
- return [a compare:b];
- }
-
- return NSOrderedSame;
- }
- ];
-
- if (sortType == FLEXTableColumnHeaderSortTypeDesc) {
- sortContentData = sortContentData.reverseObjectEnumerator.allObjects.copy;
- }
-
- self.rows = sortContentData;
- [self.multiColumnView reloadData];
- }
- #pragma mark -
- #pragma mark About Transition
- - (void)willTransitionToTraitCollection:(UITraitCollection *)newCollection
- withTransitionCoordinator:(id <UIViewControllerTransitionCoordinator>)coordinator {
- [super willTransitionToTraitCollection:newCollection withTransitionCoordinator:coordinator];
-
- [coordinator animateAlongsideTransition:^(id <UIViewControllerTransitionCoordinatorContext> context) {
- if (newCollection.verticalSizeClass == UIUserInterfaceSizeClassCompact) {
- self.multiColumnView.frame = CGRectMake(0, 32, self.view.frame.size.width, self.view.frame.size.height - 32);
- }
- else {
- self.multiColumnView.frame = CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height - 64);
- }
-
- [self.view setNeedsLayout];
- } completion:nil];
- }
- @end
|