Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/112.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 编辑表格视图并删除文档文件_Iphone_Ios_Tableview_Nsfilemanager - Fatal编程技术网

Iphone 编辑表格视图并删除文档文件

Iphone 编辑表格视图并删除文档文件,iphone,ios,tableview,nsfilemanager,Iphone,Ios,Tableview,Nsfilemanager,我有一个带有4个viewController的tabBarController。 其中之一是TableViewController。 在tableView的viewDidLoad中,我将2 NSMutableArray序列化 list = [[NSMutalbeArray alloc] init]; dates = [[NSMutalbeArray alloc] init]; 在视图中,我添加对象 NSError *error; NSError *error1; fileManager = [

我有一个带有4个viewController的tabBarController。 其中之一是TableViewController。 在tableView的viewDidLoad中,我将2 NSMutableArray序列化

list = [[NSMutalbeArray alloc] init];
dates = [[NSMutalbeArray alloc] init];
在视图中,我添加对象

NSError *error;
NSError *error1;
fileManager = [NSFileManager defaultManager];
paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
docsPath = [paths objectAtIndex:0]; 
for (NSString *file in [fileManager contentsOfDirectoryAtPath:docsPath error:&error]) {
    fileFrom = [docsPath stringByAppendingPathComponent:file];
    NSDictionary* properties = [[NSFileManager defaultManager]
                                attributesOfItemAtPath:fileFrom
                                error:&error1];
    NSDate* modDate = [properties objectForKey:NSFileModificationDate];        
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];    
    [dateFormatter setLocale: [NSLocale currentLocale]];    
    [dateFormatter setDateStyle:kCFDateFormatterMediumStyle];
    [dateFormatter setTimeStyle:kCFDateFormatterNoStyle];   
    NSString* dateString = [dateFormatter stringFromDate:modDate];
    BOOL isDir;
    BOOL exists = [[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"%@/%@", docsPath, file] isDirectory:&isDir];
    if (exists && !isDir && ![file hasPrefix:@"."]) {
        [list addObject:file];
        [dates addObject:dateString];
    }
}
[[self tableView] reloadData];    
其中每个文件都是my documentDirectory中的文件名,日期字符串是创建日期

我可以在我的tableView中可视化文件列表

如果我编辑tableView并尝试删除它工作的元素,我可以从文件系统和tableView上删除该元素,但如果我向文档目录添加一个新文件(就像应用程序那样),我会收到一个EXE\u BAD\u地址

- (void)tableView:(UITableView *)tableView commitEditingStyle:    (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
    NSString *temp = [list objectAtIndex:indexPath.row];
    NSString *lastPath = [docsPath stringByAppendingPathComponent:temp];
    [fileManager removeItemAtPath:lastPath error:nil];
    [self.list removeObjectAtIndex:indexPath.row];
    [self.dates removeObjectAtIndex:indexPath.row];
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];

        [self.tableView reloadData];
   }   
}

有什么建议吗?

当您只添加新文件而没有删除任何内容时,是否会发生崩溃?您是如何添加新文件的?我没有看到你的UITableViewCellEditingStyleInserthi,没有。我尝试删除新文件时发生崩溃。在另一个类的一个函数中,我写入文档目录文件(基本上是.csv-.gpx-.kml中的曲目):文件根据用户请求写入(曲目结束)或在applicationWillTerminate中写入(如果应用程序崩溃或用户从后台模式退出应用程序,我会记录数据)。