0

我从 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

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

4

1 回答 1

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

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

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

编辑

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

这是一个使用文件路径的辅助类,如果您有问题,请询问

于 2012-07-11T12:14:49.323 回答