我在插入新項目後重新加載我的核心數據時遇到了問題我嘗試了以下方法,但沒有喜悅。我想知道這是否可能是由於我調用數據的方式?我對這個很新,並且一直在跟着各種各樣的教程來到這裏但是我現在卡住了。我知道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
您是否嘗試過Xcode附帶的內置Core Data示例?創建一個新項目,在嚮導中選擇Master-Detail並啓用Core Data。看看這個例子。 –
@ user3246508 - 如果我的回答不滿意,請將問題標記爲已回答。 –