FLEXTableListViewController.m 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. //
  2. // PTTableListViewController.m
  3. // PTDatabaseReader
  4. //
  5. // Created by Peng Tao on 15/11/23.
  6. // Copyright © 2015年 Peng Tao. All rights reserved.
  7. //
  8. #import "FLEXTableListViewController.h"
  9. #import "FLEXDatabaseManager.h"
  10. #import "FLEXTableContentViewController.h"
  11. @interface FLEXTableListViewController ()
  12. {
  13. FLEXDatabaseManager *_dbm;
  14. NSString *_databasePath;
  15. }
  16. @property (nonatomic, strong) NSArray *tables;
  17. @end
  18. @implementation FLEXTableListViewController
  19. - (instancetype)initWithPath:(NSString *)path
  20. {
  21. self = [super initWithStyle:UITableViewStyleGrouped];
  22. if (self) {
  23. _databasePath = [path copy];
  24. _dbm = [[FLEXDatabaseManager alloc] initWithPath:path];
  25. [_dbm open];
  26. [self getAllTables];
  27. }
  28. return self;
  29. }
  30. - (void)getAllTables
  31. {
  32. NSArray *resultArray = [_dbm queryAllTables];
  33. NSMutableArray *array = [NSMutableArray array];
  34. for (NSDictionary *dict in resultArray) {
  35. [array addObject:dict[@"name"]];
  36. }
  37. self.tables = array;
  38. }
  39. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  40. {
  41. return self.tables.count;
  42. }
  43. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  44. {
  45. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FLEXTableListViewControllerCell"];
  46. if (!cell) {
  47. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
  48. reuseIdentifier:@"FLEXTableListViewControllerCell"];
  49. }
  50. cell.textLabel.text = self.tables[indexPath.row];
  51. return cell;
  52. }
  53. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  54. {
  55. FLEXTableContentViewController *contentViewController = [[FLEXTableContentViewController alloc] init];
  56. contentViewController.contentsArray = [_dbm queryAllDataWithTableName:self.tables[indexPath.row]];
  57. contentViewController.columnsArray = [_dbm queryAllColumnsWithTableName:self.tables[indexPath.row]];
  58. contentViewController.title = self.tables[indexPath.row];
  59. [self.navigationController pushViewController:contentViewController animated:YES];
  60. }
  61. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
  62. {
  63. return [NSString stringWithFormat:@"%lu tables", (unsigned long)self.tables.count];
  64. }
  65. @end