UINavigationController 使用表视图进行深入分析
我有一个 UITableView,它列出了我的文档目录的内容。我里面有一些 zip 文件。如果我触摸 UITableView
中的文件,相应的 zip 文件将被解压缩并提取到临时目录 (NSTemporaryDirectory()
) 中。
问题是如何导航我在 tableView 中提取的内容。如果假设提取的 zip 文件包含文件夹,我应该能够在 tableView 中查看它们。实际上,流程应该像向下钻取一样。
我能够提取 zip 文件,但问题是,必须在 UITableView
中导航到它们。
这是我的 didSelectRowAtIndexPath:
部分:
NSString *filePath = //filePath;
if ([[NSFileManager defaultManager]fileExistsAtPath:filePath]) {
NSLog(@"File exists at path: %@",filePath);
} else {
NSLog(@"File does not exists at path: %@", filePath);
}
NSString *tmpDir =NSTemporaryDirectory();
ZipArchive *zip = [[ZipArchive alloc] init];
BOOL result = NO;
if ([zip UnzipOpenFile:filePath]) {
//zip file is there
if ([zip UnzipFileTo:tmpDir overWrite:YES]) {
//unzipped successfully
NSLog(@"Archive unzip Success");
result= YES;
} else {
NSLog(@"Failure To Extract Archive, maybe password?");
}
} else {
NSLog(@"Failure To Open Archive");
}
if ([[NSFileManager defaultManager] fileExistsAtPath:tmpDir isDirectory:&isDir] && isDir) {
NSLog(@"Its Folder");
//Prepare to tableview.
RootViewController *rvController =[[RootViewController alloc]initWithNibName:@"RootViewController"bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:rvController animated:YES];
}
但这不起作用。它在 tableView 的文档目录中推送相同的内容。
I have a UITableView
which lists the contents of my document directory. I have some zip files in that. If I touch a file in the UITableView
, the corresponding zip file is unzipped and extracted in a temporary directory (NSTemporaryDirectory()
).
The problem is how to navigate the contents which I extracted in a tableView. If suppose, the extracted zip file contains folders, I should able to view them in a tableView. Actually the flow should be like a drill-down.
I am able to extract the zip files, but problem is, have to navigate to them in a UITableView
.
This is my didSelectRowAtIndexPath:
part:
NSString *filePath = //filePath;
if ([[NSFileManager defaultManager]fileExistsAtPath:filePath]) {
NSLog(@"File exists at path: %@",filePath);
} else {
NSLog(@"File does not exists at path: %@", filePath);
}
NSString *tmpDir =NSTemporaryDirectory();
ZipArchive *zip = [[ZipArchive alloc] init];
BOOL result = NO;
if ([zip UnzipOpenFile:filePath]) {
//zip file is there
if ([zip UnzipFileTo:tmpDir overWrite:YES]) {
//unzipped successfully
NSLog(@"Archive unzip Success");
result= YES;
} else {
NSLog(@"Failure To Extract Archive, maybe password?");
}
} else {
NSLog(@"Failure To Open Archive");
}
if ([[NSFileManager defaultManager] fileExistsAtPath:tmpDir isDirectory:&isDir] && isDir) {
NSLog(@"Its Folder");
//Prepare to tableview.
RootViewController *rvController =[[RootViewController alloc]initWithNibName:@"RootViewController"bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:rvController animated:YES];
}
But this is not working. It's pushing the same contents in the document directory in the tableView.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要使用 UINavigationController 来处理向下钻取。每次深入都是一个新的 UITableViewController。
您需要第二个 UITableViewController 子类来处理显示 zip 中包含的文件。它可能有一个 NSString 属性,它是 zip 文件夹的完整路径。它使用该目录中的文件列表作为数据源。
启动时将原来的tableView(控制器)添加到UINavigationController的rootView中。当您点击列出 zip 文件的 tableView 时,您会将第二个 UITableViewController 推送到 UINavigationController 上,并引用提取的文件(一个新文件夹?)。
请参阅Apple 的旧代码示例了解深入了解在
UINavigationController
中。另外,请查看 UINavigationController 上的文档 来自苹果公司。You need to use a UINavigationController that will handle the drill down. Each drill down is a new UITableViewController.
You need a second UITableViewController subclass that will handle displaying the files contained in the zip. It could have a NSString property that is the full path to the zip folder. It uses the list of files in that directory as the data source.
Add the original tableView (controller) to the UINavigationController's rootView at startup. When you tap the tableView that lists the zip files, you push onto the UINavigationController your second UITableViewController with a reference to the extracted files (a new folder?).
See this legacy code example from Apple about drilling down in a
UINavigationController
. Also, check out the docs on UINavigationController from Apple.