这个代码有什么问题?不从核心数据填充表!!iPad
请帮我解决这个问题(我快疯了!),我的项目正在将数据正确地保存到coredata中的sqlite db,正如在sqlite Graphical manager中检查的那样,但我似乎无法在表视图中显示它,我需要在我的viewController中显示我的表(小表而不是整个屏幕) 如果你觉得慷慨,请检查我的小样本项目 这里是*viewcontroller.h的代码 (请注意,由于tableView不是UIViewController中的实例,因此我在表视图中包含了一个IBoutlet!)这样做是否正确?, 另外,在调用“tableView”的tableView>Local声明隐藏实例变量时,我得到了7个警告这个代码有什么问题?不从核心数据填充表!!iPad,ipad,uitableview,core-data,uiviewcontroller,Ipad,Uitableview,Core Data,Uiviewcontroller,请帮我解决这个问题(我快疯了!),我的项目正在将数据正确地保存到coredata中的sqlite db,正如在sqlite Graphical manager中检查的那样,但我似乎无法在表视图中显示它,我需要在我的viewController中显示我的表(小表而不是整个屏幕) 如果你觉得慷慨,请检查我的小样本项目 这里是*viewcontroller.h的代码 (请注意,由于tableView不是UIViewController中的实例,因此我在表视图中包含了一个IBoutlet!)这样做是否
#import <UIKit/UIKit.h>
@interface CoreDataEnsaViewController : UIViewController
<UITableViewDelegate, UITableViewDataSource, NSFetchedResultsControllerDelegate> //va controller delegate??
{
UITextField *name;
UITextField *address;
UITextField *phone;
UILabel *status;
//la table
//NSMutableArray *array;
//la tabla??
UITableView *tableView;
NSFetchedResultsController *_fetchedResultsController;
NSManagedObjectContext *_context;
}
@property (nonatomic, retain) IBOutlet UITextField *name;
@property (nonatomic, retain) IBOutlet UITextField *address;
@property (nonatomic, retain) IBOutlet UITextField *phone;
@property (nonatomic, retain) IBOutlet UILabel *status;
@property (nonatomic, retain) IBOutlet UITableView *tableView;
@property (nonatomic, retain) NSFetchedResultsController *fetchedResultsController;
@property (nonatomic, retain) NSManagedObjectContext *context;
- (IBAction) saveData;
- (IBAction) findContact;
- (IBAction) showbtn:(id) sender;
@end
#导入
@接口CoreDataEnsaViewController:UIViewController
//va控制器代表??
{
UITextField*名称;
UITextField*地址;
UITextField*电话;
UILabel*状态;
//餐桌
//NSMutableArray*数组;
//拉塔布拉??
UITableView*表格视图;
NSFetchedResultsController*\u fetchedResultsController;
NSManagedObjectContext*\u上下文;
}
@属性(非原子,保留)IBOutlet UITextField*名称;
@属性(非原子,保留)IBOutlet UITextField*地址;
@属性(非原子,保留)IBOutlet UITextField*电话;
@属性(非原子,保留)IBUILabel*状态;
@属性(非原子,保留)IBUITableView*tableView;
@属性(非原子,保留)NSFetchedResultsController*fetchedResultsController;
@属性(非原子,保留)NSManagedObjectContext*上下文;
-(i)保存数据;
-(i)已发现的联系;
-(iAction)showbtn:(id)发送方;
@结束
这里是*viewController.m
#import "CoreDataEnsaViewController.h"
#import "CoreDataEnsaAppDelegate.h"
#import "ShowViewController.h"
#import "Contacts.h"
@implementation CoreDataEnsaViewController
ShowViewController *showView;
@synthesize name, address, phone, status, tableView;
@synthesize context = _context;
@synthesize fetchedResultsController = _fetchedResultsController;
-(IBAction) showbtn:(id) sender {
showView = [[ShowViewController alloc] initWithNibName:@"ShowViewController" bundle:nil];
//anima
[UIView beginAnimations:@"flipping view" context:nil];
[UIView setAnimationDuration:1];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown
forView:self.view cache:YES];
[self.view addSubview:showView.view];
[UIView commitAnimations];
}
- (void) saveData
{
CoreDataEnsaAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [appDelegate managedObjectContext];
NSManagedObject *newContact;
newContact = [NSEntityDescription insertNewObjectForEntityForName:@"Contacts" inManagedObjectContext:context];
[newContact setValue:name.text forKey:@"name"];
[newContact setValue:address.text forKey:@"address"];
[newContact setValue:phone.text forKey:@"phone"];
name.text = @"";
address.text = @"";
phone.text = @"";
NSError *error;
[context save:&error];
status.text = @"Contact saved";
}
- (void) findContact
{
CoreDataEnsaAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [appDelegate managedObjectContext];
NSEntityDescription *entityDesc = [NSEntityDescription entityForName:@"Contacts" inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDesc];
NSPredicate *pred = [NSPredicate predicateWithFormat:@"(name = %@)", name.text];
[request setPredicate:pred];
NSManagedObject *matches = nil;
NSError *error;
NSArray *objects = [context executeFetchRequest:request error:&error];
if ([objects count] == 0) {
status.text = @"No matches";
} else {
matches = [objects objectAtIndex:0];
address.text = [matches valueForKey:@"address"];
phone.text = [matches valueForKey:@"phone"];
status.text = [NSString stringWithFormat:@"%d matches found", [objects count]];
}
[request release];
}
- (NSFetchedResultsController *)fetchedResultsController {
if (_fetchedResultsController != nil) {
return _fetchedResultsController;
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Contacts" inManagedObjectContext:_context];
[fetchRequest setEntity:entity];
//NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"details.closeDate" ascending:NO];
//[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];
[fetchRequest setFetchBatchSize:20];
NSFetchedResultsController *theFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:_context sectionNameKeyPath:nil cacheName:@"Root"];
self.fetchedResultsController = theFetchedResultsController;
_fetchedResultsController.delegate = self;
[fetchRequest release];
[theFetchedResultsController release];
return _fetchedResultsController;
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
}
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation {
// Overriden to allow any orientation.
return YES;
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
self.name = nil;
self.address = nil;
self.phone = nil;
self.status = nil;
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
//tabla
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section {
id <NSFetchedResultsSectionInfo> sectionInfo = [[_fetchedResultsController sections] objectAtIndex:section];
return [sectionInfo numberOfObjects];
//return [array count];
//return [[fetchedResultsController sections] count];
}
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
Contacts *info = [_fetchedResultsController objectAtIndexPath:indexPath];
cell.textLabel.text = info.name;
cell.detailTextLabel.text = [NSString stringWithFormat:@"%@, %@", info.address, info.phone];
}
//---insert individual row into the table view---
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
//---try to get a reusable cell---
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//---create new cell if no reusable cell is available---
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Set up the cell...
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
//tabla end
- (void)dealloc {
[name release];
[address release];
[phone release];
[status release];
self.fetchedResultsController = nil;
self.context = nil;
[super dealloc];
}
#pragma mark NSFetchedResultsControllerDelegate methods
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
// The fetch controller is about to start sending change notifications, so prepare the table view for updates.
[self.tableView beginUpdates];
}
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {
UITableView *tableView = self.tableView;
switch(type) {
case NSFetchedResultsChangeInsert:
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeUpdate:
[self configureCell:[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
break;
case NSFetchedResultsChangeMove:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
// Reloading the section inserts a new row and ensures that titles are updated appropriately.
[tableView reloadSections:[NSIndexSet indexSetWithIndex:newIndexPath.section] withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type {
switch(type) {
case NSFetchedResultsChangeInsert:
[self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
// The fetch controller has sent all current change notifications, so tell the table view to process all updates.
[self.tableView endUpdates];}
@end
#导入“CoreDataEnsaViewController.h”
#导入“CoreDataEnsaAppDelegate.h”
#导入“ShowViewController.h”
#导入“Contacts.h”
@CoreDataEnsaViewController的实现
showView控制器*showView;
@综合姓名、地址、电话、状态、表格视图;
@综合上下文=_上下文;
@综合fetchedResultsController=\u fetchedResultsController;
-(iAction)showbtn:(id)发送方{
showView=[[ShowViewController alloc]initWithNibName:@“ShowViewController”捆绑包:nil];
//阿尼玛
[UIView beginAnimations:@“翻转视图”上下文:nil];
[UIView setAnimationDuration:1];
[UIView设置动画曲线:UIViewAnimationCurveEaseInOut];
[UIView setAnimationTransition:UIViewAnimationTransitionUrlDown
forView:self.view缓存:YES];
[self.view addSubview:showView.view];
[UIView委员会];
}
-(void)保存数据
{
CoreDataEnsaAppDelegate*appDelegate=[[UIApplication sharedApplication]委托];
NSManagedObjectContext*上下文=[appDelegate managedObjectContext];
NSManagedObject*新联系人;
newContact=[NSEntityDescription insertNewObjectForEntityForName:@“Contacts”在ManagedObjectContext:context中];
[newContact setValue:name.text forKey:@“name”];
[newContact setValue:address.text forKey:@“address”];
[newContact setValue:phone.text forKey:@“phone”];
name.text=@;
address.text=@;
phone.text=@;
n错误*错误;
[上下文保存:&错误];
status.text=@“已保存联系人”;
}
-(无效)已查找的联系人
{
CoreDataEnsaAppDelegate*appDelegate=[[UIApplication sharedApplication]委托];
NSManagedObjectContext*上下文=[appDelegate managedObjectContext];
NSEntityDescription*EntityDescription=[NSEntityDescription entityForName:@“Contacts”在ManagedObjectContext:context中];
NSFetchRequest*request=[[NSFetchRequest alloc]init];
[请求集合实体:entityDesc];
NSPredicate*pred=[NSPredicate谓词格式:@“(name=%@)”,name.text];
[请求集谓词:pred];
NSManagedObject*匹配项=nil;
n错误*错误;
NSArray*对象=[context executeFetchRequest:请求错误:&错误];
如果([对象计数]==0){
status.text=@“无匹配项”;
}否则{
matches=[objects objectAtIndex:0];
address.text=[匹配valueForKey:@“地址”];
phone.text=[匹配valueForKey:@“phone”];
status.text=[NSString stringWithFormat:@“%d个已找到的匹配项”,[objects count]];
}
[请求释放];
}
-(NSFetchedResultsController*)fetchedResultsController{
如果(_fetchedResultsController!=nil){
返回_fetchedResultsController;
}
NSFetchRequest*fetchRequest=[[NSFetchRequest alloc]init];
NSEntityDescription*实体=[NSEntityDescription entityForName:@“Contacts”在托管对象上下文中:_context];
[FetchRequestSetEntity:entity];
//NSSortDescriptor*排序=[[NSSortDescriptor alloc]initWithKey:@“details.closeDate”升序:否];
//[FetchRequestSetSortDescriptors:[NSArray arrayWithObject:sort]];
[fetchRequest setFetchBatchSize:20];
NSFetchedResultsController*OfftchedResultsController=[[NSFetchedResultsController alloc]initWithFetchRequest:fetchRequest managedObjectContext:_ContextSectionNameKeyPath:nil cacheName:@“Root”];
self.fetchedResultsController=offettedResultsController;
_fetchedResultsController.delegate=self;
[请求释放];
[故障诊断结果控制器释放];
返回_fetchedResultsController;
}
//实现viewDidLoad以在加载视图(通常从nib)后执行附加设置。
-(无效)viewDidLoad{
[超级视图下载];
}
-(布尔)应自动旋转指针面定向:(UIInterfaceOrientation)interfaceOrientation{
//覆盖以允许任何方向。
返回YES;
}
-(无效)未收到记忆警告{
//如果视图没有superview,则释放该视图。
[超级记忆警告];
//释放所有未使用的缓存数据、图像等。
}
-(无效)视图卸载{
self.name=nil;
self.address=nil;
self.phone=nil;
self.status=nil;
[超级视频下载];
//释放主视图的所有保留子视图。
//例如,self.myOutlet=nil;
}
//塔布拉
-(NSInteger)表格视图中的节数:(UITableView*)表格视图{
返回1;
}
-(NSInteger)表视图:(UITableView*)表视图
节中的行数:(NSInteger)节{
id sectionInfo=[[u fetchedResultsController sections]objectAtIndex:section];
返回[sectionInfo numberOfObjects];
//返回[c]数组
NSError *error = nil;
if (![[self fetchedResultsController] performFetch:&error]) {
NSLog(@"Unsolved error: %@, %@", error, [error userInfo]);
abort();
}
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions...
{
UINavigationController *navController = self.window.rootViewController
MyViewController *controller = navController.viewControllers[0];
controller.managedObjectContext = self.managedObjectContext;
return YES;
}
-(void)prepareForSegue...
{
MyOtherController *otherController = [segue destinationViewController];
controller.managedObjectContext = self.managedObjectContext;
}