FLEXTableListViewController.m 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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)viewDidLoad
  31. {
  32. [super viewDidLoad];
  33. }
  34. - (void)getAllTables
  35. {
  36. NSArray *resultArray = [_dbm queryAllTables];
  37. NSMutableArray *array = [NSMutableArray array];
  38. for (NSDictionary *dict in resultArray) {
  39. [array addObject:dict[@"name"]];
  40. }
  41. self.tables = array;
  42. }
  43. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  44. {
  45. return self.tables.count;
  46. }
  47. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  48. {
  49. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FLEXTableListViewControllerCell"];
  50. if (!cell) {
  51. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
  52. reuseIdentifier:@"FLEXTableListViewControllerCell"];
  53. }
  54. cell.textLabel.text = self.tables[indexPath.row];
  55. return cell;
  56. }
  57. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  58. {
  59. FLEXTableContentViewController *contentViewController = [[FLEXTableContentViewController alloc] init];
  60. contentViewController.contensArray = [_dbm queryAllDataWithTableName:self.tables[indexPath.row]];
  61. contentViewController.columnsArray = [_dbm queryAllColumnsWithTableName:self.tables[indexPath.row]];
  62. contentViewController.title = self.tables[indexPath.row];
  63. [self.navigationController pushViewController:contentViewController animated:YES];
  64. }
  65. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
  66. {
  67. return [NSString stringWithFormat:@"%lu tables", (unsigned long)self.tables.count];
  68. }
  69. @end