I'm trying to sort my files by date, how can I get the result into a table view?

半城伤御伤魂 提交于 2020-01-17 15:02:15

问题


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.

EDIT: 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'

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

////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:


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

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

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

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


来源:https://stackoverflow.com/questions/48346939/im-trying-to-sort-my-files-by-date-how-can-i-get-the-result-into-a-table-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!