0

在我的应用程序中,我必须存储一些数据,为此我使用了NSHomeDirectory并将数据与文档文件夹一起存储到其中。IE

NSString *filename = [[[URL1 path] lastPathComponent] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

NSString *Path=[NSHomeDirectory() stringByAppendingString:@"/Documents/"];

result=[Path stringByAppendingString:filename];

现在我希望将这些数据显示到 UITableView 中,这怎么可能我对此进行搜索并了解NSDocumentDirectory.. NSHomeDirectory 与此有何不同?我得到了这个答案

-(void)setUpTheFileNamesToBeListed{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory  error:nil];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [filePathsArray count];   
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {


//Insert this line to add the file name to the list
cell.textLabel.text = [documentsDirectory stringByAppendingPathComponent:[filePathsArray objectAtIndex:indexPath.row]];
}

这就是我得到的......但它对我的工作方式......对于 NSHomeDirectory。

4

1 回答 1

2

在上面的代码中改变

-(void)setUpTheFileNamesToBeListed{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory  error:nil];
}

对此

-(void)setUpTheFileNamesToBeListed{
    NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingString:@"/Documents/"];
    filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory  error:nil];
}
于 2012-10-30T11:38:51.927 回答