CPDFListController.m 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. //
  2. // CPDFController.m
  3. // PDFViewer
  4. //
  5. // Created by kdan on 2022/11/14.
  6. //
  7. #import <ComPDFKit/ComPDFKit.h>
  8. #import "CPDFListController.h"
  9. #import "CPDFModel.h"
  10. #import "CPDFViewController.h"
  11. @interface CPDFListController () <UITableViewDataSource,UITableViewDelegate>
  12. @property (nonatomic,strong) UITableView *tableView;
  13. @property (nonatomic,strong) NSMutableArray *dataArray;
  14. @end
  15. @implementation CPDFListController
  16. - (instancetype)initWithModel:(CPDFModel *)model {
  17. if (self = [super init]) {
  18. [self setPath:model.filePath];
  19. }
  20. return self;
  21. }
  22. - (NSArray *)dataArray {
  23. if (!_dataArray) {
  24. _dataArray = [NSMutableArray array];
  25. NSFileManager *defaultManager = [NSFileManager defaultManager];
  26. NSArray *fileArray = [defaultManager contentsOfDirectoryAtPath:self.path error:nil];
  27. for (NSInteger i = 0; i < fileArray.count; ++i) {
  28. if ([fileArray[i] isEqualToString:@".DS_Store"]) {
  29. continue;
  30. }
  31. NSString *subFilePath = [[self.path stringByAppendingString:@"/"] stringByAppendingString:fileArray[i]];
  32. CPDFModel *model = [[CPDFModel alloc] init];
  33. model.fileName = fileArray[i];
  34. model.filePath = subFilePath;
  35. [_dataArray addObject:model];
  36. }
  37. }
  38. return _dataArray;
  39. }
  40. - (void)viewDidLoad {
  41. [super viewDidLoad];
  42. // Uncomment the following line to preserve selection between presentations.
  43. // self.clearsSelectionOnViewWillAppear = NO;
  44. // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
  45. // self.navigationItem.rightBarButtonItem = self.editButtonItem;
  46. [self setTableView:[[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain]];
  47. [self.tableView setDataSource:self];
  48. [self.tableView setDelegate:self];
  49. self.tableView.rowHeight = 50;
  50. self.tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  51. self.tableView.tableFooterView = [[UIView alloc] init];
  52. [self.view addSubview:self.tableView];
  53. }
  54. #pragma mark - UITableViewDataSource
  55. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  56. return self.dataArray.count;
  57. }
  58. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  59. static NSString *MyIdentifier = @"Path";
  60. UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:MyIdentifier];
  61. if (!cell) {
  62. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:MyIdentifier];
  63. }
  64. cell.textLabel.text = [self.dataArray[indexPath.row] fileName];
  65. return cell;
  66. }
  67. #pragma mark - UITableViewDelegate
  68. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  69. BOOL isDir = NO;
  70. NSFileManager *defaultManager = [NSFileManager defaultManager];
  71. [defaultManager fileExistsAtPath:[self.dataArray[indexPath.row] filePath] isDirectory:&isDir];
  72. if (isDir) {
  73. CPDFModel *model = [[CPDFModel alloc] init];
  74. model.filePath = [[self.dataArray[indexPath.row] filePath] stringByAppendingString:@"/"];
  75. CPDFListController *pdfListController = [[CPDFListController alloc] initWithModel:model];
  76. [self.navigationController pushViewController:pdfListController animated:YES];
  77. } else {
  78. CPDFViewController *pdfViewcontroller = [[CPDFViewController alloc] initWithPath:[self.dataArray[indexPath.row] filePath]];
  79. [self.navigationController pushViewController:pdfViewcontroller animated:YES];
  80. }
  81. }
  82. @end