我试图按日期对文件进行排序,如何将结果输入表格视图?

时间:2018-01-19 17:58:15

标签: ios objective-c uitableview

我有tableview来自文档目录的.csv文件。到目前为止,我能够获取日期并对它们进行排序,但不确定如何将结果输入数组然后进入tableview

编辑:我能够将它变成一个数组,但现在当我尝试在表格视图中获取文件时,我收到了这个错误:

Terminating app due to uncaught exception 'NSInvalidArgumentException', 
reason: '-[NSURL length]: unrecognized selector sent to instance 0x1c00ba040'

代码: - 数据     NSMutableArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);

NSString *documentsDirectory = [paths objectAtIndex:0];

NSFileManager *manager = [NSFileManager defaultManager];
NSMutableArray*   fileList = [manager contentsOfDirectoryAtPath:documentsDirectory error:nil];
//--- Listing file by name sort
NSLog(@"\n File list %@",fileList);

//---- Sorting files by extension
NSMutableArray *filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory error:nil];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF EndsWith '.csv'"];
filePathsArray =  [filePathsArray filteredArrayUsingPredicate:predicate];
NSLog(@"\n\n Sorted files by extension %@",filePathsArray);

///

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {     static NSString * simpleTableIdentifier = @" SimpleTableItem";

////gettime

NSArray  *paths3 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* fullPath = [paths3 objectAtIndex:0];
fullPath = [fullPath stringByAppendingPathComponent: [self.dirList objectAtIndex:indexPath.row]];
NSLog(@"%@", fullPath);

NSMutableArray *titleArray=[[NSMutableArray alloc]init];

NSString *fileDataString=[NSString stringWithContentsOfFile:fullPath encoding:NSUTF8StringEncoding error:nil];
NSArray *linesArray=[fileDataString componentsSeparatedByString:@"\,"];


int k=0;
for (id string in linesArray)
    if(k<[linesArray count]-1){

        NSString *lineString=[linesArray objectAtIndex:k];
        NSLog(@"%@",lineString);
        NSArray *columnArray=[lineString componentsSeparatedByString:@"\,"];
        [titleArray addObject:[columnArray objectAtIndex:0]];
        k++;

    }


NSLog(@"%@",[titleArray objectAtIndex:1]);



UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier ];
if (cell == nil) {

    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];



cell.textLabel.text = [titleArray objectAtIndex:1 ];

cell.detailTextLabel.text = [self.dirList objectAtIndex:indexPath.row];


cell.textLabel.textColor = [UIColor colorWithRed:(0.0/255.0) green:(0.0/255.0) blue:(0.0/255.0) alpha:1];
cell.textLabel.font=[UIFont systemFontOfSize:8.0];

cell.detailTextLabel.font=[UIFont systemFontOfSize:15.0];
cell.detailTextLabel.textColor = [UIColor colorWithRed:(235.0/255.0) green:(120.0/255.0) blue:(33.0/255.0) alpha:1];



return cell;

}

////

- (IBAction) sortBydate: (id) sender
{

    NSError *err;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSURL *documentDirectoryURL = [fileManager URLForDirectory:NSDocumentDirectory
                                                      inDomain:NSUserDomainMask
                                             appropriateForURL:nil
                                                        create:false
                                                         error:&err];
    NSMutableArray *files = [[fileManager contentsOfDirectoryAtURL:documentDirectoryURL
                                        includingPropertiesForKeys:@[NSURLCreationDateKey]
                                                           options:0
                                                             error:&err] mutableCopy];

    [files sortUsingComparator:^(NSURL *lURL, NSURL *rURL) {
        id lDate = [lURL resourceValuesForKeys:@[NSURLCreationDateKey] error:nil][NSURLCreationDateKey];
        id rDate = [rURL resourceValuesForKeys:@[NSURLCreationDateKey] error:nil][NSURLCreationDateKey];
        return [lDate compare:rDate];
    }];

    NSLog(@"%@", files);


      self.dirList = [files mutableCopy];
    //
        self.data.reloadData;
   // NSLog(@"Sorted Array : %@",filePathsArray);

}

1 个答案:

答案 0 :(得分:0)

首先NSSearchPathForDirectoriesInDomains已过时。在您使用FileManager时,请使用与现代网址相关的API URLForDirectory:inDomain:appropriateForURL:create:error:

您的代码使用NSMutableDictionary,这是无用的,因为字典是无序的。

要对URL进行排序,您必须编写一个收集并比较NSURLCreationDateKey资源的比较器。

代码使files数组可变并对URL进行排序。

NSError *err;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSURL *documentDirectoryURL = [fileManager URLForDirectory:NSDocumentDirectory
                                                  inDomain:NSUserDomainMask
                                         appropriateForURL:nil
                                                    create:false
                                                     error:&err];
NSMutableArray *files = [[fileManager contentsOfDirectoryAtURL:documentDirectoryURL
                           includingPropertiesForKeys:@[NSURLCreationDateKey]
                                              options:0
                                                error:&err] mutableCopy];

BOOL ascending = YES;

[files sortUsingComparator:^(NSURL *lURL, NSURL *rURL) {
    NSDate *lDate, *rDate;
    [lURL getResourceValue:&lDate forKey:NSURLCreationDateKey error:nil];
    [rURL getResourceValue:&rDate forKey:NSURLCreationDateKey error:nil];
    return ascending ? [lDate compare:rDate] : [rDate compare:lDate];
}];

NSLog(@"%@", files);