2014-04-05 43 views
0

我正在研究一個簡單的核心數據應用程序,它保存列表的名稱。我正在使用帶有文本字段的UIAlertView來輸入數據。我的問題是,當用戶輸入數據並點擊保存時,它不會顯示在TableView中。但它被創造出來,然後當我重新運行它時,它在TableView中。僅在重新啓動應用程序時獲取核心數據

所以在這一點上,我很迷茫我對核心數據真的很陌生,所以如果你有任何建議,我全是耳朵。謝謝!

這裏是所有的核心數據代碼在我TableView.m:

@implementation ViewController 
    - (NSManagedObjectContext *)managedObjectContext { 
     NSManagedObjectContext *context = nil; 
     id delegate = [[UIApplication sharedApplication] delegate]; 
     if ([delegate performSelector:@selector(managedObjectContext)]) { 
      context = [delegate managedObjectContext]; 
     } 
     return context; 
    } 

    - (void)viewDidAppear:(BOOL)animated 
    { 
     [super viewDidAppear:animated]; 

     // Fetch the devices from persistent data store 
     NSManagedObjectContext *managedObjectContext = [self managedObjectContext]; 
     NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"List"]; 
     self.lists = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy]; 

     [self.tableView reloadData]; 
    } 

-(IBAction)add:(id)sender { 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Add List" message:@"Create a New Wish List" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Save", nil]; 
    [alert setAlertViewStyle:UIAlertViewStylePlainTextInput]; 
    [alert setTag:2]; 
    [alert show]; 
    alert.delegate = self; 
} 

- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    if (buttonIndex != 0 && alertView.tag == 2) { 
     UITextField *tf = [alertView textFieldAtIndex:0]; 
     NSManagedObjectContext *context = [self managedObjectContext]; 

     // Create a new managed object 
     NSManagedObject *newList = [NSEntityDescription insertNewObjectForEntityForName:@"List" inManagedObjectContext:context]; 
     [newList setValue:tf.text forKey:@"name"]; 

     NSError *error = nil; 
     // Save the object to persistent store 
     if (![context save:&error]) { 
      NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]); 
     } 

     [self dismissViewControllerAnimated:YES completion:nil]; 
    } 

    } 
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Return NO if you do not want the specified item to be editable. 
    return YES; 
} 


- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSManagedObjectContext *context = [self managedObjectContext]; 

    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     // Delete object from database 
     [context deleteObject:[self.lists objectAtIndex:indexPath.row]]; 

     NSError *error = nil; 
     if (![context save:&error]) { 
      NSLog(@"Can't Delete! %@ %@", error, [error localizedDescription]); 
      return; 
     } 

     // Remove device from table view 
     [self.lists removeObjectAtIndex:indexPath.row]; 
     [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
    } 
} 
@end 

如果你有什麼事讓我知道!謝謝!

回答

1

它看起來像你正在將你的表格數據存儲在一個名爲self.lists的數組中。這意味着,爲了讓您的表顯示數據,您需要更新該數組的內容。在創建新條目後,您看起來並不像您這樣做,因爲您的警報視圖回調不會更新self.lists的內容。

您看起來不是排序self.lists(至少在您的讀取請求中沒有排序描述符),所以您可以直接將新條目添加到數組中。這是一個可變數組,因此只需添加新對象。然後告訴表視圖,您添加了一個新行(類似於刪除條目時當前刪除行的方式)。

如果您決定開始在某個時間點對錶格進行排序,則最好從持久存儲庫更新self.lists。基本上,從viewDidLoad獲取從Core Data獲取數據的代碼,並在添加(或刪除)條目時重複這些步驟。

您可能想要看看使用NSFetchedResultsController,它旨在簡化核心數據與表視圖的結合。

+0

謝謝我明白了,謝謝你。我需要我的NSFetchedResultsController與我的UIAlertView的代碼 – Jack

相關問題