我有一个tableview和我做了一个自定义单元格与两个文本框...我希望textfields中的文本保存在核心数据,所以我创建了核心数据类为此目的我知道如何实现核心数据方法。自定义单元格与文本框和核心数据
我很难理解如何在tableview中显示单元格,以便用户输入文本,然后将其保存在核心数据中。从我的理解这些是步骤:
1 - tableview加载并显示自定义单元默认与文本字段。
2 - 用户输入文本,点击输入,并将其保存在核心数据中。
我的问题是我如何执行数字1,我无法加载视图中的数据加载,因为没有数据是!我必须先显示单元格。
任何人都可以帮助我解决这个问题,让我明白我在这里错过了什么?这是我到目前为止的代码:
- (void)viewDidLoad
{
AppDelegate *appDelegate =[[UIApplication sharedApplication] delegate];
self.managedObjectContext=[appDelegate managedObjectContext];
NSError *error;
if (![[self fetchedResultsController] performFetch:&error]) {
/*
Replace this implementation with code to handle the error appropriately.
abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
*/
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
}
[self fetchedResultsController];
[super viewDidLoad];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [[self.fetchedResultsController sections] count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[[self.fetchedResultsController sections] objectAtIndex:section] numberOfObjects];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"machines";
cellMeios *cellM = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cellM == nil) {
cellM = [[cellMeios alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
return cellM;
}
- (NSFetchedResultsController *)fetchedResultsController
{
if (_fetchedResultsController != nil)
{
return _fetchedResultsController;
}
// Create and configure a fetch request.
NSFetchRequest *fetchRequestMo = [[NSFetchRequest alloc] init];
NSEntityDescription *entityMo = [NSEntityDescription entityForName:@"Mo" inManagedObjectContext:self.managedObjectContext];
[fetchRequestMo setEntity:entityMo];
// Create the sort descriptors array.
NSSortDescriptor *cellTitle = [[NSSortDescriptor alloc] initWithKey:@"reference" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:cellTitle, nil];
[fetchRequestMo setSortDescriptors:sortDescriptors];
// Create and initialize the fetch results controller.
_fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequestMo managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"reference" cacheName:nil];
_fetchedResultsController.delegate = self;
self.fetchedResultsController = _fetchedResultsController;
return _fetchedResultsController;
}
还有其他方法,但我认为他们是不相关的我的问题,事情是...我需要的东西显示给用户,使他可以填补!
我已经有了另一个tableview处理核心数据,但在这种情况下,表首先是空的,然后在tableview顶部有一个加号按钮,然后他选择了一些东西,然后get's保存在显示包含相关信息的单元的核心数据中。
在此先感谢 此致敬礼。
你好史蒂芬,首先感谢你回答我的帖子,i'm真的卡在这一个。你这样说:“当fetchedResultsController回来时,你也可以触发这个方法”......我怎么能执行这个?我需要引用我的单元格吗?我想要做的是最好的方法? – Japa 2013-04-06 21:20:00
实质上,对于用户来说有一些东西需要编辑,一个对象必须已经存在,然后进行编辑。此方法是在模型和表格中插入新对象的示例,以便它至少包含一个项目。在您的视图控制器中实现NSFetchedResultsControllerDelegate方法controllerDidChangeContent:将在调用完成时调用它。如果您发现提取没有返回结果,请直接调用insertNewObject。这将使用新对象重新加载表格,很可能在屏幕上显示任何内容之前。 – 2013-04-06 22:27:10