Ios I';我正在按日期对文件进行排序,如何将结果放入表视图中?
我有一个Ios I';我正在按日期对文件进行排序,如何将结果放入表视图中?,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我有一个tableview,其中充满了文档目录中的.csv文件。到目前为止,我能够获取日期并对它们进行排序,但不确定如何将结果放入数组,然后放入表视图中 编辑:我可以将其放入数组中,但现在当我尝试在表视图中获取文件时,出现以下错误: Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSURL length]: unrecognized selector sent to inst
tableview
,其中充满了文档目录中的.csv文件。到目前为止,我能够获取日期并对它们进行排序,但不确定如何将结果放入数组,然后放入表视图中
编辑:我可以将其放入数组中,但现在当我尝试在表视图中获取文件时,出现以下错误:
Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: '-[NSURL length]: unrecognized selector sent to instance 0x1c00ba040'
代码:
--资料
NSMutableArray*Path=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,是)
///
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
静态NSString*simpleTableIdentifier=@“SimpleTableItem”
首先,nsSearchPathForDirectorIEsIndomain
已经过时。当您使用FileManager
时,请使用与URL相关的现代APIURLForDirectory:inDomain:ApproverForURL:create:error:
您的代码使用了NSMutableDictionary
,这是无用的,因为字典是无序的
要对URL进行排序,您必须编写一个比较器来收集和比较NSURLCreationDateKey
资源
代码使文件
数组可变,并将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);
太棒了,谢谢!现在我正在尝试获取表视图中的文件,但是我得到了一个错误,代码:self.dirList=[files mutableCopy];self.data.reloadData;错误:由于未捕获的异常“NSInvalidArgumentException”,终止应用程序,原因:'-[NSURL length]:发送到实例0x1c00ba040'***的无法识别的选择器第一次抛出调用堆栈:编辑问题并添加相关代码。错误消息显示某个地方使用了字符串API,但接收者是URL。谢谢,我如何解决此问题?可能是将URL直接分配到标签的文本属性。您不能这样做,URL不是string.assignurl.lastPathComponent
或url.absoluteString
。您应该在ObjC中使用轻量级泛型,然后编译器会帮助您在编译时避免这些错误。有意义,好的,我看到lastPathComponent返回文件名,但不确定如何在代码中实现我如何实现它?谢谢
////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);
}
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);