2014-02-24 62 views
3

我在插入新項目後重新加載我的核心數據時遇到了問題我嘗試了以下方法,但沒有喜悅。我想知道這是否可能是由於我調用數據的方式?我對這個很新,並且一直在跟着各種各樣的教程來到這裏但是我現在卡住了。我知道viewdidappear被稱爲nslog說明這一點,但表格未更新使用核心數據刷新表格視圖

`- (void)viewDidAppear:(BOOL)animated{ 
    [super viewDidAppear:animated]; 
    [self.tableView reloadData]; 

    NSLog (@"did appear"); 
}` 

任何想法?

// ViewController.m 
// Core Data 

#import "ViewController.h" 
#import "AppDelegate.h" 

@interface ViewController() 
@property (weak, nonatomic) IBOutlet UINavigationItem *back; 
@property (weak, nonatomic) IBOutlet UITableView *tableView; 

@property (strong, nonatomic) NSMutableArray *name; 
@property (strong, nonatomic) NSMutableArray *phone; 

@end 

@implementation ViewController 


- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 



    self.name = [[NSMutableArray alloc] init]; 
    self.phone = [[NSMutableArray alloc] init]; 

    AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; 
    NSManagedObjectContext *context = [appDelegate managedObjectContext]; 
    NSEntityDescription *entityDesc = [NSEntityDescription entityForName:@"Contact" inManagedObjectContext:context]; 
    NSFetchRequest *request = [[NSFetchRequest alloc] init]; 
    [request setEntity:entityDesc]; 
// NSPredicate *pred =[NSPredicate predicateWithFormat:@"(name = %@)", @"Vea Software"]; 
// [request setPredicate:pred]; 
    NSManagedObject *matches = nil; 

    NSError *error; 
    NSArray *objects = [context executeFetchRequest:request 
               error:&error]; 

    if ([objects count] == 0) 
    { 
     NSLog(@"No matches"); 
    } 
    else 
    { 
     for (int i = 0; i < [objects count]; i++) 
     { 
      matches = objects[i]; 
      [self.name addObject:[matches valueForKey:@"name"]]; 
      [self.phone addObject:[matches valueForKey:@"phone"]]; 
     } 
    } 

} 

- (void)viewDidAppear:(BOOL)animated{ 
    [super viewDidAppear:animated]; 
    [self.tableView reloadData]; 

    NSLog (@"did appear"); 
} 


- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 



- (IBAction)add:(id)sender 
{ 
    AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; 

    NSManagedObjectContext *context = [appDelegate managedObjectContext]; 
    NSManagedObject *newContact; 
    newContact = [NSEntityDescription insertNewObjectForEntityForName:@"Contact" inManagedObjectContext:context]; 

    addentry = self.addtextbox.text; 

    [newContact setValue: addentry forKey:@"name"]; 
    [self.name addObject: addentry]; 
    [newContact setValue: @"(555) 555 - 5555" forKey:@"phone"]; 
    [self.phone addObject:@"(555) 555 - 5555"]; 
    NSError *error; 
    [context save:&error]; 
    [self.tableView reloadData]; 
} 

#pragma Table View 

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return self.name.count; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *cellID = @"cellID"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID]; 
    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID]; 
    } 
    cell.textLabel.text = [self.name objectAtIndex:indexPath.row]; 
    return cell; 
} 



@end 
+0

您是否嘗試過Xcode附帶的內置Core Data示例?創建一個新項目,在嚮導中選擇Master-Detail並啓用Core Data。看看這個例子。 –

+0

@ user3246508 - 如果我的回答不滿意,請將問題標記爲已回答。 –

回答

2

在本教程http://www.raywenderlich.com/999/core-data-tutorial-for-ios-how-to-use-nsfetchedresultscontroller

你需要重新思考你的視圖控制器的架構看看。在一般情況下,你想:

  • 具有視圖控制器加載數據,只是「坐在那裏」等待接收,事情發生了變化的通知,並重新加載基於該通知。這是「UI線程」,不應該被數據訪問操作阻止

  • 您的數據更改發生在後臺。現代應用程序使用大中央調度,使在後臺這些操作,但也有其他的選擇,比如的NSOperation,線程等

  • NSFetchedResultsController爲您處理UI更新。讓核心數據上下文管理數據合併。

這兩個概念是讓事情按照他們應該工作的關鍵。否則,你可能會得到一堆代碼。

祝你好運!