在 UITableView 中显示 plist 文件

发布于 2024-10-17 11:44:39 字数 1630 浏览 7 评论 0原文

因此,我的应用程序由一个视图控制器中的文本视图组成。当用户点击保存按钮时,该文本视图中的文本将使用以下代码写入 plist:

- (IBAction)saveDoc:(id)sender

{ NSString *typedText = TypingArea.text; NSMutableArray *totalFiles = [[NSMutableArray alloc] init];

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"savedFiles.plist"];

[totalFiles addObject:typedText];

[totalFiles writeToFile:path atomically:YES];
[totalFiles release];
}

然后,在一个完全独立的视图控制器中,我有一个 UITableView,它使用以下代码显示保存的文件:

- (void)viewDidLoad {

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectories = [paths objectAtIndex:0];
NSString *path = [documentsDirectories stringByAppendingPathComponent:@"savedFiles.plist"];

NSLog(@"Got Path: %@", documentsDirectories);




array = [[NSMutableArray alloc] initWithContentsOfFile:path];
NSLog(@"Loaded Array into new array: %@", array);

[super viewDidLoad];

// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
//self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

数组是一个 NSMutableArray。然后我将其加载到表视图中,如下所示:

// Configure the cell...

    NSString *cellValue = [array objectAtIndex:indexPath.row];
    cell.textLabel.text = cellValue;

我的问题是,如何制作才能保存多个文件并在表视图中显示多个文件。目前,只能保存一个文件,并且每次按下保存按钮时都会覆盖该文件。

提前致谢,

泰特

So, my app consists of a textview in one view controller. When the user taps a save button, the text from that textview is written to a plist with this code:

- (IBAction)saveDoc:(id)sender

{
NSString *typedText = typingArea.text;
NSMutableArray *totalFiles = [[NSMutableArray alloc] init];

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"savedFiles.plist"];

[totalFiles addObject:typedText];

[totalFiles writeToFile:path atomically:YES];
[totalFiles release];
}

Then, in a totally separate view controller, I have a UITableView that displays the saved file using this code.:

- (void)viewDidLoad {

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectories = [paths objectAtIndex:0];
NSString *path = [documentsDirectories stringByAppendingPathComponent:@"savedFiles.plist"];

NSLog(@"Got Path: %@", documentsDirectories);




array = [[NSMutableArray alloc] initWithContentsOfFile:path];
NSLog(@"Loaded Array into new array: %@", array);

[super viewDidLoad];

// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
//self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

array is an NSMutableArray. Then I load it into the table view like this:

// Configure the cell...

    NSString *cellValue = [array objectAtIndex:indexPath.row];
    cell.textLabel.text = cellValue;

My question is, how do I make it so I can save multiple files and show multiple files in the table view. Right now, only one file can be saved and it is overwritten every time the save button is pressed.

Thanks in advance,

Tate

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

荆棘i 2024-10-24 11:44:39

我的策略通常是创建一个 NSDictionary 并保存文件,其中键作为文件名,对象作为数组。然而,在你这样做之前,我认为你需要检查当前位置是否有东西在里面。

为此:

NSDictionary currentData = [NSDictionary dictionaryWithContentsOfFile:path];
[currentData setValue:totalFiles forKey:@"firstArray"];
[currentData writeToFile:path atomically:YES];

您当然可以自己即兴创作。查看 NSDictionary 的文档,在我看来,它是一种非常有用的数据类型(但可能会变得非常复杂)。

否则为什么不用另一个 plist 文件呢?即使用savedFile.plist和savedFile2.plist或类似的东西?

My strategy is usually to make an NSDictionary and save the files with the key as the name of the file, and the object as your array. However before you do that one I think you need to check whether the current location has something inside.

To do that:

NSDictionary currentData = [NSDictionary dictionaryWithContentsOfFile:path];
[currentData setValue:totalFiles forKey:@"firstArray"];
[currentData writeToFile:path atomically:YES];

You can of course improvise yourself. Check out documentation for NSDictionary, it's a very useful data type IMO (but can get very complicated).

Otherwise why don't you just have another plist file? i.e. use savedFile.plist and savedFile2.plist or something like that?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文