Ios 使用核心数据刷新表视图
我在插入新项目后重新加载我的核心数据时遇到问题。我尝试了以下方法,但没有任何乐趣。我想知道这是否是因为我调用数据的方式?我对这一点很陌生,一直遵循各种教程走到今天,但我现在卡住了。我知道viewdidappear被称为nslog,但该表未更新Ios 使用核心数据刷新表视图,ios,objective-c,Ios,Objective C,我在插入新项目后重新加载我的核心数据时遇到问题。我尝试了以下方法,但没有任何乐趣。我想知道这是否是因为我调用数据的方式?我对这一点很陌生,一直遵循各种教程走到今天,但我现在卡住了。我知道viewdidappear被称为nslog,但该表未更新 `- (void)viewDidAppear:(BOOL)animated{ [super viewDidAppear:animated]; [self.tableView reloadData]; NSLog (@"did a
`- (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
//ViewController.m
//核心数据
#导入“ViewController.h”
#导入“AppDelegate.h”
@界面视图控制器()
@属性(弱,非原子)IBUINAVIGATIONITEM*返回;
@属性(弱、非原子)IBUITableView*tableView;
@属性(强,非原子)NSMutableArray*名称;
@属性(强,非原子)NSMutableArray*电话;
@结束
@实现视图控制器
-(无效)viewDidLoad
{
[超级视图下载];
self.name=[[NSMutableArray alloc]init];
self.phone=[[NSMutableArray alloc]init];
AppDelegate*AppDelegate=[[UIApplication sharedApplication]委托];
NSManagedObjectContext*上下文=[appDelegate managedObjectContext];
NSEntityDescription*EntityDescription=[NSEntityDescription entityForName:@“Contact”inManagedObjectContext:context];
NSFetchRequest*request=[[NSFetchRequest alloc]init];
[请求集合实体:entityDesc];
//NSPredicate*pred=[NSPredicate PREDITEWITHFORMAT:@“(名称=%@)”,@“Vea软件”];
//[请求集谓词:pred];
NSManagedObject*匹配项=nil;
n错误*错误;
NSArray*对象=[context executeFetchRequest:请求
错误:&错误];
如果([对象计数]==0)
{
NSLog(@“无匹配项”);
}
其他的
{
对于(int i=0;i<[对象计数];i++)
{
匹配=对象[i];
[self.name addObject:[匹配valueForKey:@“name”];
[self.phone addObject:[匹配valueForKey:@“phone”];
}
}
}
-(无效)视图显示:(BOOL)动画{
[超级视图显示:动画];
[self.tableView重载数据];
NSLog(@“确实出现”);
}
-(无效)未收到记忆警告
{
[超级记忆警告];
//处置所有可以重新创建的资源。
}
-(iAction)添加:(id)发件人
{
AppDelegate*AppDelegate=[[UIApplication sharedApplication]委托];
NSManagedObjectContext*上下文=[appDelegate managedObjectContext];
NSManagedObject*新联系人;
newContact=[NSEntityDescription insertNewObjectForEntityForName:@“Contact”在ManagedObjectContext: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”];
n错误*错误;
[上下文保存:&错误];
[self.tableView重载数据];
}
#pragma表视图
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节
{
返回self.name.count;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
静态NSString*cellID=@“cellID”;
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:cellID];
如果(单元格==nil)
{
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault重用标识符:cellID];
}
cell.textlab.text=[self.name objectAtIndex:indexath.row];
返回单元;
}
@结束
看看本教程
您需要重新考虑视图控制器的体系结构。通常,您希望:
- 让视图控制器加载数据,然后“坐在那里”等待接收事情已更改的通知,然后根据该通知重新加载。这是“UI线程”,不应被数据访问操作阻止
- 让您的数据更改发生在后台。现代应用程序使用Grand Central Dispatch在后台执行这些操作,但还有其他选择,如NSOperation、线程等
- 让NSFetchedResultsController为您处理UI更新。让核心数据上下文管理数据整合
祝你好运 您是否尝试过Xcode附带的内置核心数据示例?创建新项目,在向导中选择“主详细信息”并启用核心数据。请查看示例。@user3246508-如果我下面的答案令人满意,请将问题标记为已回答。