gpt4 book ai didi

ios - 读取 plist 文件以填充表格时出错

转载 作者:行者123 更新时间:2023-11-29 13:51:20 24 4
gpt4 key购买 nike

我正在为 iPhone 开发一个应用程序,但我遇到了一个问题...
我有一个带有一些文本字段的 View ,其中写入的信息保存在一个 plist 文件中。使用@class 和#import 声明,我将这个 View Controller 导入到另一个管理 TableView 的 Controller 中。
我刚刚编写的代码似乎是正确的,但我的表中填满了 3 个相同的行...
我不知道为什么这行是 3...

谁能帮帮我?

这是添加信息的代码:

@implementation AddWishController

@synthesize titleField, linkField, descField;

-(NSString *)dataFilePath {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return [documentsDirectory stringByAppendingPathComponent:plistPath];
}

-(IBAction)done {
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject:titleField.text];
[array addObject:linkField.text];
[array addObject:descField.text];
[array writeToFile:[self dataFilePath] atomically:YES];

[array release];

[self dismissModalViewControllerAnimated:YES];
}

这控制了表格:

#import "WishlistController.h"
#import "AddWishController.h"

@implementation WishlistController

@synthesize lista;

-(IBAction)newWish {
AddWishController *add = [[AddWishController alloc] initWithNibName:@"AddWish" bundle:nil];
[self.navigationController presentModalViewController:add animated:YES];
[add release];
}

-(NSString *)dataFilePath {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return [documentsDirectory stringByAppendingPathComponent:plistPath];
}

#pragma mark -
#pragma mark View lifecycle

- (void)viewDidLoad {
[super viewDidLoad];
}

#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSArray *data = [[NSArray alloc] initWithContentsOfFile:[self dataFilePath]];
return [data count];
[data release];
}


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

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}

NSString *filePath = [self dataFilePath];
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath];
cell.textLabel.text = [array objectAtIndex:0];
cell.detailTextLabel.text = [array objectAtIndex:2];

NSLog(@"%@", array);
}

return cell;
}

最佳答案

您的第一个问题是 NSArray writeToFile: 不会附加到现有文件。它只是将数组的完整内容写入文件。如果同名文件已经存在,则该函数将覆盖它。

这意味着在您目前的代码中,您只能保存一个包含三个元素的数组。无论用户在 AddWishController 的 View 中保存多少次,磁盘上只会保留最后捕获的三条信息。

其次,您的 WishlistController 配置错误。

这个:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSArray *data = [[NSArray alloc] initWithContentsOfFile:[self dataFilePath]];
return [data count];
[data release];
}

将始终返回三的计数,因为文件中的数组始终具有三个元素。但是,即使那样也是错误的,因为您不想在单独的单元格中显示每个元素。您的 cellForRowAtIndexPath: 非常清楚地将数组的第一个和最后一个元素放入每个单元格中。

至少在这里,您需要一个二维数组。更好的是你需要一系列字典。每个字典将在 -[AddWishController done] 中保存一个“完成”操作的结果。将每个字典添加到一个数组中。然后在您的 TableView Controller 中,返回表中行数的数组计数。然后在 cellForRowAtIndexPath: 中,您应该在 indexpath.row 的数组元素中获取字典。然后从字典中的每个条目获取单元格 UI 元素的文本。

通常,在应用程序退出之前,您也不会保存到文件。相反,将数组放在应用程序委托(delegate)的属性中。在 AddWishController 中创建一个属性来引用应用程序委托(delegate)属性。在 TableView Controller 中执行相同的操作。现在您可以在第一个 Controller 中填充数组并从第二个 Controller 读取它。

基本上,您需要从头开始。

关于ios - 读取 plist 文件以填充表格时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2984385/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com