CPDFController.m 3.4 KB

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