2011-12-14 93 views
1

我有一个带有类别列表的tableview。如果用户在添加按钮上点击带有文本框的alertview并且出现“ok”按钮,并且在文本框中输入的文本应该被添加到表格视图中。我能够添加它,但是它没有被添加到plist.so如果我们转向另一种观点,回来就会消失,如何让它保持那样。将动态输入的数据保存到plist

-(void)viewDidLoad 
{ 
    NSString *fileForCategoryList = [[NSBundle mainBundle] pathForResource:kCATEGORY_LIST ofType:kP_List]; 
    self.arrayForCategories = [[NSMutableArray alloc]initWithContentsOfFile:fileForCategoryList]; 
} 

- (void)tableView:(UITableView *)aTableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    if (editingStyle == UITableViewCellEditingStyleDelete) 
    { 
     [self.arrayForCategories removeObjectAtIndex:indexPath.row]; 
     [self.tableViewC reloadData]; 
    } 
    else if (editingStyle == UITableViewCellEditingStyleInsert) 
    { 
     UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:kYOUR_TITLE message:kEMPTY delegate:self cancelButtonTitle:kCANCEL otherButtonTitles:kOK, nil]; 
     self.myTextField = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 45.0, 260.0, 25.0)]; 
     [self.myTextField setBackgroundColor:[UIColor whiteColor]]; 
     [myAlertView addSubview:self.myTextField]; 
     [myAlertView show]; 
     [myAlertView release]; 
    } 
} 

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex 
{ 
    if ([self.myTextField text]) 
    { 
     [self.arrayForCategories addObject:[self.myTextField text]]; 
    } 
    [self.tableViewC reloadData]; 
} 

回答

1

您必须将该数据写入plist文件并保存。希望你从应用程序的包中访问文件。制作它的副本,并将其保存到Documents目录中。然后,当您添加新数据时,通过写入文档目录的plist文件将该数据保存到plist中。

[yourData writeToFile:filePath atomically:YES]; 

更新

看是否有此tutorial帮助。

EDIT

首先读取来自plist中的数据到一个数组(可变,这样就可以将对象添加到它)。然后再加入所有对象写这个数组到文件后的新数据添加到使用

[yourArray addObject:someObject]; 

这个数组。

+0

你能给我一点代码或任何相关的链接吗? – Chandu 2011-12-14 10:38:20