2011-12-14 97 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