iOS不限级文件列表

    源码下载:http://www.oschina.net/code/snippet_616092_14113    sql

    前段时间用UITableView作了一个获取document下的树形文件列表,不一样于网上其余用plist或sqllite预约义好文件level,该程序根据文件夹层次,自动分级别。该文件列表的实现思路,以及代码以下。数组

    一、首先,建立工程,并添加rootViewController,在rootViewController中添加UITableView控件,设置其dataSource及delegate为self或者任何其余类。
spa

    二、UITableViewCell是根据路径肯定显示的labelText.text的内容的,因此,咱们须要获取每一个UITableViewCell对应的路径,另外,返回上级目录的时候,也是根据路径及级别reload,tableView的。因此,咱们先建立一个包含level,和pathName两个字段的数据模型CellModel.net

    三、肯定数据模型后,根据给定的初始path设置cellModel对象,并把该对象存到全局的NSMutableArray里。code

   self.modelArray = [[NSMutableArray alloc] init];component

    CellModel * cellmode = [[CellModel alloc] init];对象

    cellmode.pathName = FILELIST_DIR;//FILELIST_DIR是个宏定义,定义了初始路径。ip

    cellmode.level = 0;rem

    [self.modelArray addObject:cellmode];源码

    [cellmode release];

    四、获取某个路径下的文件及文件夹:

        NSArray * tempArray = (NSArray *)[[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:&error];

    判断这个文件是不是文件夹;

     BOOL isDir = NO;

    [[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&isDir];

    五、关于列表的加载和刷新,返回,其实就是更换全局可变数组modelArray里面的数据。而modelArray的数据是根据你每次点击的UITabelViewCell肯定的。最关键的代码在UITableView的delegate方法- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath,该方法主要做用是根据点击的cell对应的cellModel对象reload对应的数据。该方法代码以下:

static int levels = 0;//文件的级别。

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    CellModel * cellmodel = (CellModel *)[modelArray objectAtIndex:indexPath.row];//得到当前文件目录路径

    levels = cellmodel.level;//获得文件级别

    path = cellmodel.pathName; 

    NSError * error = [[NSError alloc] init];

    NSArray * tempArray = (NSArray *)[[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:&error];//获得该目录的子目录

    NSArray * array = [NSArray arrayWithArray:self.modelArray];

    for (CellModel * model in array) 

    {

        if(model.level > levels)

        {

            [self.modelArray removeObject:model];//根据目录级别,删除大于该级别的文件

        }

    }

    BOOL isDir = NO;

    [[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&isDir];

    if(isDir)//判断点击的是否为文件夹

    {

    int count = indexPath.row + 1;

    NSMutableArray * files = [NSMutableArray arrayWithArray:tempArray];

    for(NSString * fileName in tempArray)

    {

        if([fileName rangeOfString:@"."].length != NSNotFound)//判断获取的文件是不是以.开头的,是的话就删掉

        {

            NSArray * array = [fileName componentsSeparatedByString:@"."];

            if(([array count] == 2)&&[[array objectAtIndex:0] isEqualToString:@""])

            {

                [files removeObject:fileName]; 

            }

        } 

    }

    for(NSString * fileName in files)

    {

        NSString * pathName = [path stringByAppendingPathComponent:fileName];

        CellModel * model = [[CellModel alloc] init];

        model.pathName = pathName;

        model.level = indexPath.row + 1;//在此设置子文件的级别

        [self.modelArray insertObject:model atIndex:count++];//把子文件对象插入到数组中

        [model release];

    }

        NSArray * mement = [NSArray arrayWithArray:self.modelArray];

        for(CellModel * model in mement)

        {

            if((model.level == levels) && (![model.pathName isEqualToString:path]))

            {

                [self.modelArray removeObject:model];//删掉同级目录

            }

        }

        [tableView reloadData];

    }

}

相关文章
相关标签/搜索