我正在尝试按日期对文件进行排序,如何将结果放入表格视图中? [英] I'm trying to sort my files by date, how can I get the result into a table view?

查看:21
本文介绍了我正在尝试按日期对文件进行排序,如何将结果放入表格视图中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a tableview that's is been filled with .csv files from document directory. So far I'm able to get the date and sort them but not sure how to get the result in to an array and then into the tableview.

我可以将它放入一个数组中,但是现在当我尝试在表视图中获取文件时出现此错误:

i'm able to get it into an array but now when i try to get the files in the table view i got this error:

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

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

Code: --data 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";

-(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);

}

推荐答案

首先 NSSearchPathForDirectoriesInDomains 已经过时了.当您使用 FileManager 时,请使用现代 URL 相关 API URLForDirectory:inDomain:properForURL:create:error:

First of all NSSearchPathForDirectoriesInDomains is outdated. As you are using FileManager anyway use the modern URL related API URLForDirectory:inDomain:appropriateForURL:create:error:

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

Your code uses NSMutableDictionary which is useless because dictionaries are unordered.

要对 URL 进行排序,您必须编写一个比较器来收集和比较 NSURLCreationDateKey 资源.

To sort URLs you have to write a comparator which gathers and compares the NSURLCreationDateKey resource.

代码使 files 数组可变,并对 URL 进行适当排序.

The code make the files array mutable and sorts the URLs in place.

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);

这篇关于我正在尝试按日期对文件进行排序,如何将结果放入表格视图中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆