我有一个表格视图,我制作了一个带有两个文本字段的自定义单元格...我希望将文本字段中的文本保存在核心数据中,所以我为此目的创建了核心数据类,并且我知道如何实现核心数据方法.
我无法理解如何在表格视图中显示单元格,以便用户输入文本,然后将其保存在核心数据中。据我了解,这些是步骤:
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;
}
还有其他方法,但我认为它们与我的问题无关,问题是......我需要向用户显示一些东西,以便他可以填写!
我已经有另一个处理核心数据的表格视图,但在这种情况下,表格一开始应该是空的,然后表格视图顶部有一个加号按钮,然后他选择了一些东西,它被保存在核心数据中显示具有相关信息的单元格。
预先感谢问候。