我有一個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