【问题标题】:How can I save each time a new data to my plist file rather than replace the old one?如何每次将新数据保存到我的 plist 文件而不是替换旧数据?
【发布时间】:2012-07-11 12:05:18
【问题描述】:

我从 plist 文件中读取数据并将数据添加到 tableview

我的列表:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
    <dict>
        <key>Title</key>
        <string>Section1</string>
        <key>Rows</key>
        <array>
            <string>Section1 Item1</string>
            <string>Section1 Item2</string>
        </array>
    </dict>
    <dict>
        <key>Title</key>
        <string>Section2</string>
        <key>Rows</key>
        <array>
            <string>Section2 Item1</string>
            <string>Section2 Item2</string>
        </array>
    </dict>
</array>
</plist>

.h

#import "RootViewController.h"

@interface RootViewController ()

@property (copy, nonatomic) NSArray* tableData;

@end

.m

@implementation RootViewController

@synthesize tableData;

- (void) dealloc
{
    self.tableData = nil;
    [super dealloc];
}

- (void) viewDidLoad
{
    [super viewDidLoad];
    self.tableData = [NSArray arrayWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @"Table" ofType: @"plist"]];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
{
    return [tableData count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{
    return [[[tableData objectAtIndex: section] objectForKey: @"Rows"] count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
{
    return [[tableData objectAtIndex: section] objectForKey: @"Title"];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    static NSString *CellIdentifier = @"Cell";

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

    cell.textLabel.text = [[[tableData objectAtIndex: indexPath.section] objectForKey: @"Rows"] objectAtIndex: indexPath.row];

    return cell;
}

@end

如何在每次写入新数据而不是替换旧数据时写入该字符串和数组?

【问题讨论】:

    标签: ios xcode uitableview save plist


    【解决方案1】:

    使用此代码,您可以读取 plist 文件:

    self.tableData = [NSArray arrayWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @"Table" ofType: @"plist"]];
    

    然后你写:

    [self.tableData writeToFile:path atomically:YES];
    

    如果您不想添加更多项目,只需添加到self.tableData,当您写入文件时,它将被添加到 plist。

    唯一的问题是你需要写在文档目录而不是捆绑包上。 该捆绑包是您的应用程序文件夹,由应用商店签名和批准。要编辑捆绑包中的文件,您应该先复制它,然后使用该副本,如下所示:

      NSError *error;
      NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
      NSString *documentsDirectory = [paths objectAtIndex:0];
      NSString *path = [documentsDirectory stringByAppendingPathComponent:[@"Table" stringByAppendingPathExtension:@"plist"]];
    
      NSFileManager *fileManager = [NSFileManager defaultManager];
    
      if (![fileManager fileExistsAtPath: path]) {
        NSString *bundle = [[NSBundle mainBundle] pathForResource:name ofType:@"plist"];
    
        [fileManager copyItemAtPath:bundle toPath: path error:&error];
      }
    

    此代码末尾的 path 是文档目录中 plist 文件的路径,与您的 bundle 中的 Table.plist 完全相同。

    path这个文件你可以读写,[[NSBundle mainBundle] pathForResource: @"Table" ofType: @"plist"]这个文件你只能读。

    编辑

    基本上你需要做这样的事情: https://gist.github.com/3090009

    这是一个处理文件路径的辅助类,如果您有问题,请直接询问

    【讨论】:

      猜你喜欢
      • 2017-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-25
      • 1970-01-01
      • 2013-12-15
      • 2021-10-06
      • 2018-03-06
      相关资源
      最近更新 更多