iphone - 在 UITableView 中显示 NSHomeDirectory() 保存的文件

标签 iphone objective-c ios uitableview

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

NSString *filename = [[[URL1 路径] 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。

最佳答案

修改上面的代码

-(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];
}

关于iphone - 在 UITableView 中显示 NSHomeDirectory() 保存的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13138130/

相关文章:

iphone - 根据手势重新排列 UIIview

c# - 二维 NSMutableArray 有帮助吗?

objective-c - 确定应用程序是否因系统关闭而终止

ios - 保存和加载 NSMutableArray - NSUserDefaults

ios - Xamarin.iOS CoreBluetooth/外部附件问题

iphone - Xcode 4.0 验证错误 CFBundleVersion

ios - ENABLE_BITCODE 在 xcode 7 中做了什么?

iphone - 为自定义 UIView 设置背景图像

ios - Swift 将图像附加到 iMessage

iphone - 是否可以知道 iPhone 何时进入休眠状态?