// // CPDFController.m // PDFViewer // // Created by kdan on 2022/11/14. // #import #import "CPDFListController.h" #import "CPDFModel.h" #import "CPDFViewController.h" @interface CPDFListController () @property (nonatomic,strong) UITableView *tableView; @property (nonatomic,strong) NSMutableArray *dataArray; @end @implementation CPDFListController - (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 = 0; i < fileArray.count; ++i) { if ([fileArray[i] isEqualToString:@".DS_Store"]) { continue; } 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 { BOOL isDir = NO; NSFileManager *defaultManager = [NSFileManager defaultManager]; [defaultManager fileExistsAtPath:[self.dataArray[indexPath.row] filePath] isDirectory:&isDir]; if (isDir) { CPDFModel *model = [[CPDFModel alloc] init]; model.filePath = [[self.dataArray[indexPath.row] filePath] stringByAppendingString:@"/"]; CPDFListController *pdfListController = [[CPDFListController alloc] initWithModel:model]; [self.navigationController pushViewController:pdfListController animated:YES]; } else { CPDFViewController *pdfViewcontroller = [[CPDFViewController alloc] initWithPath:[self.dataArray[indexPath.row] filePath]]; [self.navigationController pushViewController:pdfViewcontroller animated:YES]; } } @end