123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- //
- // CPDFController.m
- // PDFViewer
- //
- // Created by kdan on 2022/11/14.
- //
- #import <ComPDFKit/ComPDFKit.h>
- #import "CPDFController.h"
- #import "CPDFModel.h"
- @interface CPDFController () <UITableViewDataSource,UITableViewDelegate>
- @property (nonatomic,strong) UITableView *tableView;
- @property (nonatomic,strong) NSMutableArray *dataArray;
- @end
- @implementation CPDFController
- - (instancetype)initWithModel:(CPDFModel *)model {
- if (self = [super init]) {
- [self setPath:model.filePath];
- }
- return self;
- }
- - (NSArray *)dataArray {
- if (!_dataArray) {
-
- _dataArray = [NSMutableArray array];
-
- NSFileManager *defaultManager = [NSFileManager defaultManager];
- NSArray *fileArray = [defaultManager contentsOfDirectoryAtPath:self.path error:nil];
-
- for (NSInteger i = 1; i < fileArray.count; ++i) {
-
- NSString *subFilePath = [[self.path stringByAppendingString:@"/"] stringByAppendingString:fileArray[i]];
-
- CPDFModel *model = [[CPDFModel alloc] init];
- model.fileName = fileArray[i];
- model.filePath = subFilePath;
-
- [_dataArray addObject:model];
-
- }
- }
- return _dataArray;
- }
- - (void)viewDidLoad {
- [super viewDidLoad];
-
- // Uncomment the following line to preserve selection between presentations.
- // self.clearsSelectionOnViewWillAppear = NO;
-
- // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
- // self.navigationItem.rightBarButtonItem = self.editButtonItem;
-
- [self setTableView:[[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain]];
-
- [self.tableView setDataSource:self];
- [self.tableView setDelegate:self];
-
- self.tableView.rowHeight = 50;
-
- self.tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
-
- self.tableView.tableFooterView = [[UIView alloc] init];
-
- [self.view addSubview:self.tableView];
- }
- #pragma mark - UITableViewDataSource
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
- return self.dataArray.count;
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
- static NSString *MyIdentifier = @"Path";
-
- UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:MyIdentifier];
-
- if (!cell) {
- cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:MyIdentifier];
- }
-
- cell.textLabel.text = [self.dataArray[indexPath.row] fileName];
- return cell;
- }
- #pragma mark - UITableViewDelegate
- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
-
- // Get the path of a PDF
- NSString *pdfPath = [self.dataArray[indexPath.row] filePath];
-
- // Initialize a CPDFDocument object with the path to the PDF file
- NSURL *url = [NSURL fileURLWithPath:pdfPath];
- CPDFDocument *document = [[CPDFDocument alloc] initWithURL:url];
-
- // Initialize a CPDFView object with the size of the entire screen
- CPDFView *pdfView = [[CPDFView alloc] initWithFrame:self.view.bounds];
-
- // Set the document to display
- pdfView.document = document;
-
- [self.view addSubview:pdfView];
- }
- @end
|