1. 程式人生 > >IOS --- 讀取本地檔案

IOS --- 讀取本地檔案

#import "BIDDirectoryViewController.h"

@interface BIDDirectoryViewController ()
@property(nonatomic,retain)UIBarButtonItem *upload;
@end

@implementation BIDDirectoryViewController

- (id)initWithStyle:(UITableViewStyle)style WithDirectoryPath:(NSString*) p
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
        
        _upload = [[UIBarButtonItem alloc]initWithTitle:@"upload" style:UIBarButtonItemStylePlain target:self action:@selector(uploadFile:)];
        self.navigationItem.rightBarButtonItem = _upload;
        self.directoryPath = p;
        [self loadDirectoryContents];
        NSString *pathTitle = [self.directoryPath lastPathComponent];
        self.title = pathTitle;
    }
    return self;
}
-(void)uploadFile:(id) sender
{
    
}


-(void)loadDirectoryContents
{
    _directoryContents = [[NSMutableArray alloc]init];
    _directoryContents = [[NSFileManager defaultManager]directoryContentsAtPath:self.directoryPath];
}

- (void)viewDidLoad
{
    [super viewDidLoad];

   
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
    // Return the number of rows in the section.
    return [_directoryContents count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    // Configure the cell...
    
    if(cell==nil)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    NSString *selectedFile = (NSString*)[_directoryContents objectAtIndex:indexPath.row];
    BOOL isDir;
    NSString *selectedPath = [self.directoryPath stringByAppendingPathComponent:selectedFile];
    if([[NSFileManager defaultManager]fileExistsAtPath:selectedPath isDirectory:&isDir] &&isDir){
    
        cell.imageView.image = [UIImage imageNamed:@"1.png"];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }else{
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.imageView.image =[UIImage imageNamed:@"2.png"];
    }
    
    cell.textLabel.text = selectedFile;
    
    
    return cell;
}


#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.
    /*
     <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
     [self.navigationController pushViewController:detailViewController animated:YES];
     */
    
    NSString *selectedFile = (NSString*)[_directoryContents objectAtIndex:indexPath.row];
    BOOL isDir;
    NSString *selectedPath = [self.directoryPath stringByAppendingPathComponent:selectedFile];
    if([[NSFileManager defaultManager]fileExistsAtPath:selectedPath isDirectory:&isDir] &&isDir)
    {
        BIDDirectoryViewController *directoryController = [[BIDDirectoryViewController alloc]initWithStyle:UITableViewStylePlain WithDirectoryPath:selectedPath];
        [[self navigationController]pushViewController:directoryController animated:YES];
        //directoryController.directoryPath = selectedPath;
    }else{
//       
//        BIDFileOverviewViewController *fileController = [[BIDFileOverviewViewController alloc]initWithStyle:UITableViewStylePlain WithFilePath:selectedPath];
        //[[self navigationController]pushViewController:fileController animated:YES];
    }
}

@end