2012-07-11 18 views
0

我從plist文件讀取數據添加到tableview中如何將每次新數據保存到我的plist文件而不是替換舊數據?

我的plist:

<?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 

我如何寫該字符串和數組每次都是新數據而不是repl王牌老?

回答

1

有了這個代碼,你看plist文件:

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

,並用這個你寫:

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

,如果你不希望添加更多的項目只是增加self.tableData當你寫入文件將被添加到plist。

唯一的問題是,你需要在文檔目錄上編寫而不是捆綁在一起。 該捆綁包是您的應用程序文件夾,由appstore簽名並批准。編輯這是你的包,你應該先拷貝它,然後使用複製,像這樣的文件:

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文件,這將是完全相同的Table.plist在您的包。

這個文件在path你可以寫和讀,在[[NSBundle mainBundle] pathForResource: @"Table" ofType: @"plist"]你只能讀取文件。

編輯

基本上you`l需要做這樣的事情: https://gist.github.com/3090009

這是一個輔助類文件路徑的工作,如果你有問題就問

相關問題