加載視圖時,我將下列子類別管理對象加載到數組中。如何根據核心數據中的關係對象創建UITableView部分
@class Aircraft;
@interface AircraftTypes : NSManagedObject
@property (nonatomic, retain) NSNumber * isIFRCapable;
@property (nonatomic, retain) NSString * model;
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSNumber * numEngines;
@property (nonatomic, retain) NSString * type;
@property (nonatomic, retain) Aircraft *tailNumber;
@property (nonatomic, retain) NSNumber *sortOrder;
@end
和
@class AircraftTypes;
@interface Aircraft : NSManagedObject
@property (nonatomic, retain) NSString * homeStation;
@property (nonatomic, retain) NSNumber * isSimulator;
@property (nonatomic, retain) NSString * owner;
@property (nonatomic, retain) NSString * tailNumber;
@property (nonatomic, retain) AircraftTypes *aircraftType;
@property (nonatomic, retain) NSManagedObject *loggedFlights;
@end
的關係是
AircraftTypes <--->> Aircraft
我所能,並已成功地與孩子玩耍的對象中的UITableView細胞的關係中,並segueing對其進行編輯。我想要做的是如何根據AircraftType成功創建UITableView部分,並使用屬於該類型的飛機填充每個部分,並根據哪些行被刪除成功地引用原始數組(索引)。這是我迄今爲止,但我卡住了。
獲取數組:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.managedObjectContext = [(ATPAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
managedObjectContext = self.managedObjectContext;
self.aircraftList = [CoreDataHelper getObjectsForEntity:@"Aircraft" withSortKey:@"tailNumber" andSortAscending:YES andContext:self.managedObjectContext];
self.aircraftTypeList = [CoreDataHelper getObjectsForEntity:@"AircraftTypes" withSortKey:@"sortOrder" andSortAscending:YES andContext:self.managedObjectContext];
}
加載數據(這整個區塊工作至今):
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [aircraftTypeList count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
// return the title of an individual category
AircraftTypes *thisType = [self.aircraftTypeList objectAtIndex:section];
return [NSString stringWithFormat:@"%@ %@", thisType.type, thisType.model];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
AircraftTypes *thisType = [aircraftTypeList objectAtIndex:section];
int count = 0;
for (Aircraft *thisAircraft in self.aircraftList) {
if (thisAircraft.aircraftType == thisType) {
count++;
}
}
if (self.editing) { //Add insert row
count++;
}
return count;
}
這一個是造成錯誤,我做了一些數據預裝入堆棧,並保存在應用程序中成功DidFinishLaunchingWithOptions
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"AircraftCell";
ATPSettingsMyAircraftTableCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
if (cell == Nil) {
cell = [[ATPSettingsMyAircraftTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: cellIdentifier];
}
AircraftTypes *thisType = [aircraftTypeList objectAtIndex:indexPath.section];
cell.labelTailNumber.text = thisType.tailNumber.tailNumber;
cell.labelHomeStation.text = thisType.tailNumber.homeStation;
cell.labelAircraftModel.text = [NSString stringWithFormat:@"%@ %@",
thisType.type, thisType.model];
cell.labelHours.text = @"";
//calcuate aircraft hours later
return cell;
}
而且我甚至沒有開始k現在如何引用commitEditingStyle的正確對象。這是來自另一個TableViewController的片段,它不使用節並且有一個引用我的managedObject的單個數組,因此我需要知道需要對此進行哪些更改以引用正確的對象,因爲我將要處理indexPath .section and .row:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Ensure that if the user is editing a name field then the change is committed before deleting a row -- this ensures that changes are made to the correct event object.
[tableView endEditing:YES];
// Delete the managed object at the given index path.
NSManagedObject *flightConditionList = (self.flightConditionList)[indexPath.row];
[self.managedObjectContext deleteObject:flightConditionToDelete];
// Update the array and table view.
[self.flightConditionList removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
// Commit the change.
NSError *error;
if (![self.managedObjectContext save:&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]);
abort();
}
}
}
在此先感謝,如果它是如此簡單,飛行員可以做到這一點。
這是一個編輯,試圖去與第一個答案。不知道我是否正確地將實體分成AircraftTypes實體的部分,但我還需要根據aircraftTypes.type和.model命名節頭。
嘗試使用以下代碼。不知道sectionNameKeyPath是否爲飛機類型的關係實體編碼。另外,我需要從airTypes實體類型和模型的兩個屬性標記節標題。
- (NSFetchedResultsController *) fetchedResultsController {
if (fetchedResultsController != nil) {
return fetchedResultsController;
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Aircraft" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]
initWithKey:@"tailNumber" ascending:YES];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];
[fetchRequest setFetchBatchSize:20];
NSFetchedResultsController *theController = [[NSFetchedResultsController alloc]
initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"aircraftType" cacheName:@"Master"];
self.fetchedResultsController = theController;
fetchedResultsController.delegate = self;
return fetchedResultsController;
}
請參閱上面的我的編輯代碼(底部片段)來實現fetchedResultsController。我想我意識到我需要sectionNameKeyPath去aircraftTypes.type(NSString)。但是,我有多個相同「類型」的條目,因此是附加的「模型」標識符。你可以在多個關係字符串值上做一個sectionNameKeyPath嗎?即'sectionNameKeyPath:@「aircraftType.type + aircraftType.model」'?? –